cancel
Showing results for 
Search instead for 
Did you mean: 

How to access variable from scripttask

agiledevpro
Champ in-the-making
Champ in-the-making
Hi,

I'm having problem accessing a value from my scripttask in my java code.
This is my scripttask:

   <scriptTask id="parseFile" name="Execute script" scriptFormat="groovy" >
         <script><![CDATA[
                                execution.setVariable("json", "result);
            ]]></script>
      </scriptTask>


And this is how I try to access the variable from my java code:

Map <String, Object> var = processInstance.getProcessVariables();
String res = var.get("json");


But "res" is null, what am I doing wrong?


Thanks in advance!
5 REPLIES 5

agiledevpro
Champ in-the-making
Champ in-the-making
Edit:

This is my scripttask, forgott to add a ":

<code>
<scriptTask id="parseFile" name="Execute script" scriptFormat="groovy" >
   <script><![CDATA[
                                execution.setVariable("json", "result");
    ]]></script>
  </scriptTask>
</code>

penusila611621
Champ in-the-making
Champ in-the-making
Java code means, which listener class you are trying. You can always use task.getVariable("json") in user task listener OR execution.getVariable("json") in system listeners.

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

Have a look on following jUnit test in activiti source org.activiti.examples.bpmn.scripttask.ScriptTaskTest#testSetScriptResultToProcessVariable
Regards
Martin

agiledevpro
Champ in-the-making
Champ in-the-making
Hi, thank you for your answer.

Is it possible to return a value from a scripttask without using a listener?
And if I have to use a listener how can I access the variable from outside of my listener in another class?

Regards

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,
There is no need to use listener - see jUnit test mentioned above.

Regards
Martin