cancel
Showing results for 
Search instead for 
Did you mean: 

Set form variable scope as local

adityap174
Champ in-the-making
Champ in-the-making
My use case as following :
An array of objects enters the process. The process then goes through a loop task, with loopCardinality as the size of the array. Each execution instance of the loop is then assigned an object as a local variable. Now, each of these instances then go through an user task, which gets completed using form submission. What I want is the value received from the form to be set as a local variable. In the DefaultFormHandler of the activiti code base, I found out that the form submission code has the line

execution.setVariable(propertyId, propertiesCopy.get(propertyId))

which sets the incoming form variable as a global variable. The problem with that is that the new form submission overwrites the value of the form variables of the previous execution instance.  What I want is to store the form variables received in each loop as a local variable of that loop instance, without adding another script task to achieve this. I couldn't find any option to change the default behavior. Is there something I can do to achieve this ?
5 REPLIES 5

adityap174
Champ in-the-making
Champ in-the-making
Forgot to mention. Activiti version is 5.15.1

frederikherema1
Star Contributor
Star Contributor
You can't change the behaviour of the form-properties for the moment, I'm afraid. What you can do is add a task-listener (on complete) that copies those properties from the execution-scope to the task-scope. Since you're in the same transaction when you're listener is executed, you're 100% sure the values submitted on the current execution are the ones from the task that just completed…

You an create some kind of generic listener that inspects what properties could have been submitted and copies those, regardless of the task-type…

anjan
Champ in-the-making
Champ in-the-making
Sorry for updating an existing request.  We also have similar requirement.  In the solution suggested above, it is mentioned that a task listener can be used to copy the property from execution scope to the task scope.  I am not sure how to do this.  On completion of the current task, I do have the value of the property, but how do I add a task local variable to the next task that is not yet created.  Please suggest.

trademak
Star Contributor
Star Contributor
You can't add a variable to the next task, instead Activiti works with process or scoped variables to achieve this. You can copy a local variable to the process or other scoped context by invoking runtimeService.setVariable. This will also make sure that the variable is available in the next task (and any next activity in the process).

Best regards,

anjan
Champ in-the-making
Champ in-the-making
Thanks Tijs for the update.  Our use case is that certain set of follow up tasks need to be completed based on the variable.  If the variable is updated, the the next set of follow up tasks and all the previous follow up tasks(which are still pending) will have the updated variable value which is not desired.

So I thought of using Sub Process and the above behaviour is same for the embedded sub process.  Instead I switched to a Standalone sub process.  Now it is working correctly.  I pass the required variables from the main process to the sub process and the variable update on the main process will not impact the sub process.