cancel
Showing results for 
Search instead for 
Did you mean: 

Global variables of multi-instance and callActivity?

ryu
Champ in-the-making
Champ in-the-making
activitiSmiley Surprisedut in a callActivity allows the possibility to write a result from a subprocess back to its parent process.
However, activitiSmiley Surprisedut does not work in a multi-instance of callActivity, which means there are no ways to write the result back to the main process when you are in a loop.

I think it is very important. One of the Scenario I can think of, is to terminate the loop under some result receiving from a sub-process. For example, when an activity inside a subprocess fails, you may want to set a variable to say the loop to terminate immediately (with <completionCondition>)…

[size=150]Solution[/size]:
One possible solution I've already tested successfully, is to pass a list from our main to the sub process (well, you can say, pass by the value of a reference)
[attachment=1]activiti.jpg[/attachment]
<callActivity id="callactivity1" name="Call activity" calledElement="subProcess">
      <extensionElements>
        <activiti:in source="list" target="list"></activiti:in>
      </extensionElements>     
      <multiInstanceLoopCharacteristics isSequential="true">
        <loopDataInputRef>…</loopDataInputRef>
        <completionCondition>${list.size()!=0}</completionCondition>
      </multiInstanceLoopCharacteristics>
</callActivity>
By doing this way, every changes to the list inside the subprocess will be visible to the main process. List is now like a global variable for the main and all sub process of the loop. Also, I can trigger the loop to terminate immediately when, for example, the list is not empty as above.

[size=150]Problems:[/size]
But another problem, when you use a userTask inside the subprocess, then whatever changes to the list before the userTask, the list will be empty after that.
[attachment=0]activiti2.jpg[/attachment]
I guess, at the userTask, a transaction is completed and all data are persisted to the database. After that, the list we want to reference to, may be not the same list anymore. The above scenario is for sequential loop. For a parallel loop, it is a little bit different. But I want to discuss about sequential loop first. Any ideas to solve the problems would be helpful.

thank you
6 REPLIES 6

jbarrez
Star Contributor
Star Contributor
The simplest solution imo would be to allow for expressions in the activitiSmiley Surprisedut, allowing to write variables like activitiSmiley Surprisedut="myVar-${loopCounter}". Would that cover your use case?

ryu
Champ in-the-making
Champ in-the-making
I'm very glad that you replied to this topic.
Your suggestion is to write variable from every sub process to different variables of the main process. It's a good idea,. But sadly, the problem is, the element activitiSmiley Surprisedut does not work. activitiSmiley Surprisedut does not write any thing when being used in a multi-instance, even a value like this:

        <activitiSmiley Surprisedut sourceExpression="${&quot;HelloWorld&quotSmiley Wink}" target="i"></activitiSmiley Surprisedut>
i is the variable of the main process. If it works,  it doesn't matter how many times i is overwritten, we should have "HelloWorld" in i in the end. But not.

So when I looked back in the source code of MultiInstanceActivityBehavior, seems like there are no functions to set variables to its parent process when a sub process "leave"

jbarrez
Star Contributor
Star Contributor
Then that is indeed a but. Could you create a jira issue for it so we can track it?

ryu
Champ in-the-making
Champ in-the-making

chockyc91
Champ in-the-making
Champ in-the-making
Can anyone suggest solution for this issue. I am unable to access the output parameter from callActivity in Multi instance loop.
I want to create parallel tasks dynamically using callActivity and if any one of the tasks is Rejected, I want to complete all the tasks and instance.

Late reply but might be useful for someone else....

One way of dealing with it would be to write a delegate or script to do the following...

execution below is from the subprocess

superExecutionId = execution.getSuperExecutionId();
runtimeService.setVariable(superExecutionId, someVariableName, someValue);