cancel
Showing results for 
Search instead for 
Did you mean: 

Persisting modified variables in scriptTask

thomasl
Champ in-the-making
Champ in-the-making
Hello,

I start a process with an output variable which is a basic HashMap. In a javascript scriptTask, I insert some values into this map as follows:


output.put("result", "success");

If I read the process variables inside an ExecutionListener at the end of the process, the result value is as expected :


public void notify(DelegateExecution execution) throws Exception {
   String result = ((Map) execution.getVariable("output")).get("result"); // success

However, the modified variable is not persisted in the DB, a HistoricVariableInstance query returns the variable state at the beginning of the process. To have the variable persisted, I have to force it manually:


output.put("result", "success");
execution.setVariable("output", output);

Is it the intended behavior ? Do I have to manually persist all variables modifications ?

Regards,

Tom

3 REPLIES 3

trademak
Star Contributor
Star Contributor
Are you aware of the autoStoreVariables attribute (described here http://www.activiti.org/userguide/index.html#bpmnScriptTask) ?
You have to explicitly set the value to true if you want to persist values automatically in a script task.

Best regards,

thomasl
Champ in-the-making
Champ in-the-making
Hi Tijs,

Thanks for your answer. Yes I had tested with this attribute and found out that it works only for variables defined in the script. In my script, the variable in already defined prior to scriptTask execution. With this attribute, modifications are persisted only if I do:
<code>
<scriptTask id="success"  activiti:autoStoreVariables="true" scriptFormat="javascript">
output.put("result", "success");
output = output;
</code>

Could it be that modifications should be detected and are not ? Please let me know if you would like a testcase to exhibit this behaviour.

Regards,

Tom

trademak
Star Contributor
Star Contributor
Hi Tom,

Good point, I think Activiti isn't able to see the changes to the HashMap. For now I guess we have to go for the explicit call.
Yes, you can create a JIRA for this with a test showing the issue.

Best regards,