Dynamically adding task at runtime

Is there any way to add dynamic task to any running process instance in particular order ?

For Eg: We have running process instance for invoice having step start-1-2-3-end
And we want to add 1 more step at runtime like this start-1-2-3-4-end

Hi @SantoshiP,

you can model the new version of your process, deploy it to the engine and use process instance migration to contine the process instances on the new model: https://docs.camunda.org/manual/7.11/user-guide/process-engine/process-instance-migration/.

Hope this helps, INgo

1 Like

Hi @Ingo_Richtsmeier ,

I followed your instructions but I need your help to do the final migration. So let me describe first what I’ve done so far.

I have a JavaDelegate which is responsible to add dynamically tasks in the model. When it gets executed, it makes a clone of the execution.getProcessInstance().getBpmnModelInstance() and performs any modifications there (adds tasks dynamically in the model). Finally I have an updated BpmnModelInstance which I can deploy it by doing:

ProcessEngines.getDefaultProcessEngine().getRepositoryService()
        .createDeployment()
        .addModelInstance("new-resource-namne", modelInstance)
        .deploy();

I’m trying now to do the migration, but I can’t find how to get the targetProcessDefinitionId. I’m trying to do the following:

MigrationPlan migrationPlan = ProcessEngines.getDefaultProcessEngine().getRuntimeService()
        .createMigrationPlan(execution.getProcessDefinitionId(), "what-to-put-here")
        .mapEqualActivities()
        .build();

Could you help? thanks!

Ok the following solution worked:

DeploymentWithDefinitions deployment = processEngine.getRepositoryService()
        .createDeployment()
        .name("a-name")
        .addModelInstance("a-name.bpmn", dynamicTaskModelUpdater.getModelInstance())
        .deployWithResult();

ProcessDefinition targetProcessDefinition = deployment.getDeployedProcessDefinitions().get(0);

MigrationPlan migrationPlan = processEngine.getRuntimeService()
        .createMigrationPlan(execution.getProcessDefinitionId(), targetProcessDefinition.getId())
        .mapEqualActivities()
        .build();

processEngine.getRuntimeService()
        .newMigration(migrationPlan)
        .processInstanceIds(execution.getProcessInstanceId())
        .execute();
2 Likes

hi, does this influence start new instance with the original process definition?