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

tomi87
Champ in-the-making
Champ in-the-making
Ok thank you. I deleted the @Autowired private RuntimeService runtimeService;

Now its going further.
But I get still the error message that the value of studentInfo.getAdvisorName() is null:
      System.out.println("sould be Bernd Hauch: " + studentInfo.getaddName());

Now I tried it with:
List<HistoricDetail> historyVariables = activitiRule.getHistoryService().createHistoricDetailQuery().variableUpdates()
         .orderByVariableName().asc().list();
    assertNotNull(historyVariables);

    assertEquals(1, historyVariables.size());
    HistoricVariableUpdate stAppUpdate = ((HistoricVariableUpdate) historyVariables.get(0));

    assertEquals("stAppInfo", stAppUpdate.getVariableName());
    StAppInfo  saii = (StAppInfo) stAppUpdate.getValue();
    //saii.getAdvisorName() is null
    assertEquals("Bernd Hauch", saii.getAddName());
But it says the same that saii.getAdvisorName() is null.

Ho can I get access to my stored values of the variables addName and addEmail ?

frederikherema1
Star Contributor
Star Contributor
Could you perhaps write a unit-test (look at the sticky in the forum with good guidance how to do this) and upload this somewhere we can access it? Because pasting snippets back and forth won't take us anywhere…

tomi87
Champ in-the-making
Champ in-the-making
I put it into a zip. Here:
https://www.dropbox.com/s/7vdamm700650jse/propertyTestTomi87.zip

I have problems to use Gists.

Hope it's ok for you.

tomi87
Champ in-the-making
Champ in-the-making
I dont know if this works but I created now here:
https://github.com/tomi87/propertyTestTomi87.git

frederikherema1
Star Contributor
Star Contributor
Discovered what the issue is here. A serializable-value is handled differently than any other variables, since it can be altered in-memory. They are, when the context closes, checked for modification and flushed to the database, without an explicit "setVariable()" command. The code involved:


public void flush() {
    // this first check verifies if the variable value was not overwritten with another object
    if (deserializedObject==variableInstanceEntity.getCachedValue()) {
      byte[] bytes = SerializableType.serialize(deserializedObject, variableInstanceEntity);
      if (!Arrays.equals(originalBytes, bytes)) {
        variableInstanceEntity
          .getByteArrayValue()
          .setBytes(bytes);
      }
    }
  }

This works as expected, but down not update the historic detail-value, because there is no explicit "setVariable()" command called, so no historic detail can be recorded. If you would have an additional (service/receive) tasks after the task, you would see that the RUNTIME variable is indeed altered. 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")".

tomi87
Champ in-the-making
Champ in-the-making
Hey thank you for the tipp.
But It doesn't work right.

<scriptTask id="scrpt" name="Execute script" scriptFormat="groovy">
<script>
execution.setVariable(execution.getVariable("stInfo")
</script>
</scriptTask>

You are sure that it should: getVariable("stInfo") ?

And I get this error:
org.activiti.engine.ActivitiException: problem evaluating script: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token:  @ line 1, column 60.
   on.getVariable("stInfo")
                                  ^

1 error

at org.activiti.engine.impl.scripting.ScriptingEngines.evaluate(ScriptingEngines.java:71)
at org.activiti.engine.impl.bpmn.behavior.ScriptTaskActivityBehavior.execute(ScriptTaskActivityBehavior.java:51)
at org.activiti.engine.impl.pvm.runtime.AtomicOperationActivityExecute.execute(AtomicOperationActivityExecute.java:44)
at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)

frederikherema1
Star Contributor
Star Contributor
Perhaps end with an ';'

unexpected token:  @ line 1, column 60.

tomi87
Champ in-the-making
Champ in-the-making
I tried it with an ";" too. But the same error message.

org.activiti.engine.ActivitiException: problem evaluating script: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token:  @ line 1, column 61.
   n.getVariable("stInfo");
                                 ^

1 error

at org.activiti.engine.impl.scripting.ScriptingEngines.evaluate(ScriptingEngines.java:71)
at org.activiti.engine.impl.bpmn.behavior.ScriptTaskActivityBehavior.execute(ScriptTaskActivityBehavior.java:51)
at org.activiti.engine.impl.pvm.runtime.AtomicOperationActivityExecute.execute(AtomicOperationActivityExecute.java:44)
at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
at org.activiti.engine.impl.pvm.runtime.AtomicOperationTransitionNotifyListenerStart.eventNotificationsCompleted(AtomicOperationTransitionNotifyListenerStart.java:52)
at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:56)
at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)
at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:530)
at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:49)
at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:85)
at org.activiti.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:535)

frederikherema1
Star Contributor
Star Contributor
Please CHECK your code before posting to forum, I'm not a groovy-compiler/interpreter Smiley Tongue. You're missing a closing bracket!

tomi87
Champ in-the-making
Champ in-the-making
OMG! Thank you!

But now I get this error:

org.activiti.engine.ActivitiException: problem evaluating script: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.activiti.engine.impl.persistence.entity.ExecutionEntity.setVariable() is applicable for argument types: (null) values: [null]
Possible solutions: getVariable(java.lang.String), setVariable(java.lang.String, java.lang.Object), setVariables(java.util.Map), getVariables(), hasVariable(java.lang.String), hasVariables()
at org.activiti.engine.impl.scripting.ScriptingEngines.evaluate(ScriptingEngines.java:71)
at org.activiti.engine.impl.bpmn.behavior.ScriptTaskActivityBehavior.execute(ScriptTaskActivityBehavior.java:51)
at org.activiti.engine.impl.pvm.runtime.AtomicOperationActivityExecute.execute(AtomicOperationActivityExecute.java:44)
at org.activiti.engine.impl.interceptor.CommandContext.performOperation(CommandContext.java:80)