cancel
Showing results for 
Search instead for 
Did you mean: 

store a scriptTask variable

tomi87
Champ in-the-making
Champ in-the-making
Hello I try do define a variable with a value in a scriptTask, then I want to set the variable in a userTask to a object, and then to overwrite the old object with the new variable in an other scriptTask.
scriptTask:
variable= 123456789
UserTask:
<activiti:formProperty id="variable"   expression="#{object.variable}" type="long" />
ScriptTask:   
<script><![CDATA[execution.setVariable("object", execution.getVariable("object"));]]></script>

But if i trie to test this, it's doesn't work the variable isn't stored in the object.
Is there maybe an other way to store a variable from a scriptTask in in this object ?

Best regards
tomi87
8 REPLIES 8

frederikherema1
Star Contributor
Star Contributor
<activiti:formProperty id="variable" expression="#{object.variable}" type="long" />

The name of the variable you should update is "variable", not "object"…

tomi87
Champ in-the-making
Champ in-the-making
I understand It like this:

<activiti:formProperty id="variable" expression="#{object.variable}" type="long" />

–> store the variable "variable" in the Object "object" with the value which is given in the scriptTask.

<script><![CDATA[execution.setVariable("object", execution.getVariable("object"));]]></script>

–>update the Object "object" with the setted variable "variable".

Or do I have some wrong thoughts ?

Cause I don't know exactly  what do you mean…
By the way, is there a more easy way to set the variable with the scriptTask and upload it to the  object ?

frederikherema1
Star Contributor
Star Contributor
No you've lost me…


<activiti:formProperty id="variable" expression="#{object.variable}" type="long" />
The line above will: show a form-proprty of type long. When submitted in, the value filled in will be done using the expression. This will look for a process-variable named "object" and set the "variable" property, effectively calling object.setVariable(longValueFilledIn). This can ONLY WORK if there is already a variable named "object" in the process. If not, you'll get an exception.


<script><![CDATA[execution.setVariable("object", execution.getVariable("object"));]]></script>
The script-task will do nothing useful. If will read the "object" variable and write it again immediately without changing the value.

But what is your use case actually? Seems to me you're trying to use form-properties on a ScriptTask, which just doesn't work. Form-properties can only be used on UserTasks and StartEvents. All other activities are unaware of form-properties and should just use process-variables.

tomi87
Champ in-the-making
Champ in-the-making
The variable named "object" is already in this project.
I use this in my UserTask :
<activiti:formProperty id="variable" expression="#{object.variable}" type="long" />

In my test I test If the variable property for "variable", which i set in my scriptTask (<script>studentIdNumber = 123456788;</script> )
is stored into the Object "object".

I think there is an other way to set this variable, without using a userTask with activiti:formProperty.

I want to initialize the variable "variable" with a value and immediately set it into the "object". Can you suggest me a way ?

frederikherema1
Star Contributor
Star Contributor
Sure, you can use our API to start a process, providing the "object" as a variable in runtimeService.startProcessInstanceByXXX(id, variables)

tomi87
Champ in-the-making
Champ in-the-making
I don't know what you exactly mean.

Is there maybe a way to set the variable in the object just with a scriptTask?

frederikherema1
Star Contributor
Star Contributor
Sure thing… The "execution" object is available in your scripts. You can call "execution.setVariable()" in your script.

tomi87
Champ in-the-making
Champ in-the-making
Thx I get it.