cancel
Showing results for 
Search instead for 
Did you mean: 

write and read a variable from an object

tomi87
Champ in-the-making
Champ in-the-making
I don't know why it's doesn't work, to write the variable in the object and then to read it.

   <userTask id="chooseAddName" name="Choose add Name" activiti:candidateGroups="st">
         <extensionElements>
            <activiti:formProperty id="addName" name="Add name" expression="#{stAppInfo.addName}" required="true" />
         </extensionElements>
      </userTask>

I test it with this input:

StAppInfo stInfo= new StAppInfo ();
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("stAppInfo ", stInfo);
variables.put("addName", "Bernd Hauch");


If I ask which addName is inside it gives me null:
assertEquals("Bernd Hauch", stInfo.getAddName());

Why?
25 REPLIES 25

jbarrez
Star Contributor
Star Contributor
The error is quite clear:

org.activiti.engine.impl.persistence.entity.ExecutionEntity.setVariable() is applicable for argument types: (null) values: [null]

You are passing null as parameter into the setVariable method.

tomi87
Champ in-the-making
Champ in-the-making
Yes this is what I get befor I add "execution.setVariable(execution.getVariable("stInfo"))" too.

  HistoricVariableUpdate stAppUpdate = ((HistoricVariableUpdate) historyVariables.get(0));

            assertEquals("stAppInfo", stAppUpdate.getVariableName());
            StAppInfo  saii = (StAppInfo) stAppUpdate.getValue();

            assertEquals("Bernd Hauch", saii.getAddName());
//But "saii.getAdvisorName()" is null

But I submit the variable "addName" with the value "Bernd Hauch":
  Map<String, String> formProperties = new HashMap<String, String>();
  formProperties.put("addName", "Bernd Hauch");

https://github.com/tomi87/propertyTestTomi87

I can't see what I'm doing wrong, to check, if this value is "stored" in the object.

….. Workaround for this is to add an additional service-task (or script-task) after the userTask, that does nothing more than call "execution.setVariable(execution.getVariable("stInfo"))".
But it still doesn't work.

jbarrez
Star Contributor
Star Contributor
Ok, I took the time to check your code. There are a few things

- Your variable name is wrong. You start the process instance with 'variables.put("stAppInfo", stInfo);' but you use another variable name in the script
- When you set a variable, you need to provide a name for the variable. Just calling setVariable(object) does not work as there is no such method
- It's better to use CDATA around your script to avoid problems with quotes in the script

That being said, this is how it should be

              
<scriptTask id="scrpt" name="Execute script" scriptFormat="groovy">
   <script><![CDATA[
    execution.setVariable("test", execution.getVariable("stAppInfo"));
    ]]></script>
  </scriptTask>

tomi87
Champ in-the-making
Champ in-the-making
OK finally It's working. Thank you very much   Smiley Very Happy   Smiley Very Happy   Smiley Very Happy

tomi87
Champ in-the-making
Champ in-the-making
I have another question. But just for understanding this.
Why it's doens't work if I set a userTask (or another Task)
between the Task where I set the variable:
<activiti:formProperty id="addName" name="Add name" expression="#{stAppInfo.addName}" required="true" />

and the scriptTask where I call:  <script><![CDATA[
          execution.setVariable("test", execution.getVariable("stAppInfo"));
          ]]></script>

In this userTask there happens nothing. It's just like this:
<userTask id="idnumber" name="name"
  activiti:candidateGroups="student">
</userTask>

Then I can't find the test "object" in the
HistoricVariableUpdate stAppUpdate= ((HistoricVariableUpdate) historyVariables.get(0));
There is just stAppInfo which variable addName is null.

frederikherema1
Star Contributor
Star Contributor
In the user task in-between, the variable isn't explicit ally set again using another value, so no historic update is recorded…