Script task result Variable not working with custom object

Dear all,

we found an issue with Camunda Script Task. We use the script task with freemarker script language. Then we can set a result variable that holds the result of the freemarker script.

However, if we just set a regular process variable, the result gets easily written to that variable. But when we have an object with a field we are not able to store the result to that field. When we use groovy it is easy to set the variable using the dot notation, e.g.

processVariables.mytext = 'test'

works in groovy. The complete task looks like

But when we use processVariables.mytext as a result variable does not work. Instead a new process Variable with the name “processVariables.mytext” is created.

<scriptTask id="templateScript" scriptFormat="freemarker" camunda:resultVariable="processVariables.mytext">
 <script>
   Dear ${customer},

   thank you for working with Camunda BPM ${version}.

   Greetings,
   Camunda Developers
 </script>
</scriptTask>

Is that a bug? Or a missing implementation?

Hi,

I suspect the difference is, in your script task, the result variable is a string literal. Hence the string literal is being used as a process variable name.

Perhaps you need to use an expression (eg JUEL) which would look something like ${processVariables.mytext}. The trick here is to ensure that the expression can resolve the object references…

Failing that, an input/output mapping may give you the flexibility to map a local variable containmg the freemarker output to your composite process variable…

regards

Rob

Hi @Webcyberrob,

thanks for your ideas. JUEL is not working in the result Variable. If I use ${processVariables.mytext} then a new process Variable with the name “${processVariables.mytext}” is created. Thus you’re right that the result variable is interpreted as a string literal by default.

Re your second idea I used as a local result variable one with the name “result”. Using an output parameter with the name processVariables.mytext and the value result then does neither work.

The only way it works is using a groovy script output parameter and setting it like

processVariables.mytext = result

Apart from that the scriptTask result Variable is not a local variable! Therefore the “result” variable stays in the process variables for the rest of the process and must be deleted manually in the output parameter script as well. That’s not a good idea.

However, this is a step backwards as on inputs the dot notation works easily. So why not when writing values back. Further, if you use the modeler templates, the cool stuff is that you can set output parameters easy that write the values back in the process. This would no longer be possible with an additional output parameter, as output parameters are not longer available in the modeler once a template is used.

Is that a missing feature maybe @Philipp_Ossler or anyone from @camunda can help?

Hi @Tristan1,

currently, it’s not possible to add the script result to an existing variable (i.e. a POJO). The resultVariable attribute accepts just the name of the variable as string and not an expression.

Maybe you can work around this limitation:

  • create a local variable before the script task (e.g. as input variable)
  • set the local variable as result variable of the script
  • use an output mapping or a listener to add the result to the existing variable object

Best regards,
Philipp

@Philipp_Ossler

thanks for this clarification. Is it planned to include this in one of the next releases? I would open a JIRA ticket on this as there might be others who have this problem.

Hi @Tristan1,

currently, nothing is planned in this direction. Feel free to open a ticket or provide a pull request (higher chance that this feature will be a part of the engine soon).

Best regards,
Philipp

Hi @Philipp_Ossler

thanks - do you have starting point where the parsing of the result variable is located in the engine? I could imagine to provide a pull request then.

Hi @Tristan1,

that is great :+1:

The BPMN process is parsed in the class BpmnParse. Before you start, let’s discuss and make it clear, that is the goal and how can you reach it.

As far as I understand, you have a script task and want to store the result of the script. You don’t want to store the result in a new variable, instead you want to add the result to an existing POJO variable. Is this correct?

I think the solution is not just to allow an expression for the resultVariable attribute. Maybe it doesn’t work and it’s not aligned to the other usage of expression attributes.
A way could be to allow that the result variable is only local or transient (similar to the decision result). Based on this, you could use an output mapping or listener to add the result to the POJO variable.

What do you think?

Best regards,
Philipp