cancel
Showing results for 
Search instead for 
Did you mean: 

How to create stand-alone user task with local variables

andrey_nikolov
Champ in-the-making
Champ in-the-making
I am trying to create a stand-alone user task which is not associated with any process instance. How could I create local variables to be associated with the new task?

Using the code below we can create and persist the task, but the last call 'CreateVariablesLocal' causes 'lazy loading outside command context' exception due to context check in class 'VariableScopeImpl' method 'ensureVariableInstancesInitialized'.

Please advise!
<!–break–>

   TaskEntity taskEntity = (TaskEntity) taskService.newTask();
   taskEntity.setCategory(…);
   taskEntity.setDescription(…);
   taskEntity.setDueDate(…);
   taskEntity.setName(…);
   taskEntity.setOwner(…);
   taskEntity.setPriority(…);
   taskEntity.setTenantId(…);
   taskService.saveTask(taskEntity);
   taskEntity.createVariablesLocal(… map with variable keys and values …);



   org.activiti.engine.ActivitiException: lazy loading outside command context
   at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.ensureVariableInstancesInitialized(VariableScopeImpl.java:65)
   at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.createVariableLocal(VariableScopeImpl.java:746)
3 REPLIES 3

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi Andrey,

have a look on org.activiti.engine.test.api.task.TaskServiceTest#testSaveTaskUpdate in activiti source.

Regards
Martin

andrey_nikolov
Champ in-the-making
Champ in-the-making
Hi Martin,

The distributable activiti-engine-5.21.0.jar does not have package org.activiti.engine.test.api so we cannot check the code.

However we found a work-around: create each variable individually (setVariableLocal) instead of creating them in bulk (createVariablesLocal) by passing variables map.

This is somewhat counter intuitive and looks like a bug. Please investigate and advise if we should raise a ticket.

Once again, thanks for your response!

<code>
TaskEntity taskEntity = (TaskEntity) taskService.newTask();
taskEntity.setCategory(…);
taskEntity.setDescription(…);
taskEntity.setDueDate(…);
taskEntity.setName(…);
taskEntity.setOwner(…);
taskEntity.setPriority(…);
taskEntity.setTenantId(…);
taskService.saveTask(taskEntity);
for (… each variable) {
  taskService.setVariableLocal(… task ID, … variable name, … variable value);
}
</code>

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi Andrey,

this test is on Activiti6 (sorry) But there should be similar in Activiti5.
bug:
createVariableLocal must be called inside the command context.

Regards
Martin