cancel
Showing results for 
Search instead for 
Did you mean: 

Start and access new process instance from running process

flurina
Champ in-the-making
Champ in-the-making
After upgrading from Activiti 5.10 to 5.14 we're facing a problem with a ScriptTask of a running process that needs to start another ProcessInstance.
As you can see in the code below, we want to start a new ProcessInstance and afterwards access its first task to add a CandidateGroup to it.


RepositoryService runtimeService = ActivitiUtil.getRuntimeService();
TaskService taskService = ActivitiUtil.getTaskService();
      
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("MyProcess");

Task firstTask = taskService.createTaskQuery().executionId(processInstance.getProcessInstanceId()).singleResult();
firstTask.addCandidateGroup(myUsergroups);


It all worked fine while we were using Activiti 5.10 but ever since we upgraded to 5.14 we get a NullPointerException when calling addCandidateGroup, because the TaskQuery doesn't return anything. It worked with version 5.10 and still does in the JUnit-Test. Since we haven't changed anything else but upgraded to 5.14, we're assuming that there is no problem with the started process itself, but rather the time of the flush. It seems to us that the Activiti-Engine executes the flush after the script task is finished. We would need it to be executed right after calling startProcessInstanceByKey though. Is there any way to achieve that? Or do you know another approach of how to start a new process and access it from within a running process?

Thank you in advance,
Flurina
4 REPLIES 4

trademak
Star Contributor
Star Contributor
Can you share the process definition? If it's possible to create a unit test showing the issue, that would be even better.

Best regards,

flurina
Champ in-the-making
Champ in-the-making
I uploaded a Maven project showing the issue. You can find it here https://www.dropbox.com/sh/k79hjzmu407jfvs/SEy-aY23DS .
As you will see, the test does not finish now, because the ScriptTask cannot access the newly created ProcessInstance.
If you change the Activiti version back to 5.10 everything will run through smoothly and the test will succeed. I don't think that the issue is located in version 5.14 though, as the same problem already occurs from version 5.11 on.

Thanks a lot,
Flurina

trademak
Star Contributor
Star Contributor
Thanks for uploading your project that makes it a lot easier to discuss.
The problem is that the process that's started by the script task is not yet committed to the database before the "parent" process reaches a wait state. So that's why you won't get a user task back.
I changed your example, to include a second asynchronous script task that does the task query. So the first script task is only starting the process instance. And that works fine. Instead of using a script task to start the other process instance you could also use a call activity.

Best regards,

flurina
Champ in-the-making
Champ in-the-making
Thanks a lot for helping with the issue. We were able to resolve it by using two asynchronous script tasks, as you suggested.

Kind regards,

Flurina