cancel
Showing results for 
Search instead for 
Did you mean: 

Setting process variable at run time

niyas_m
Champ in-the-making
Champ in-the-making
Hi,

  I am new  to Activiti . I would like to know how to set process variables to an existing process instance ( already instantiated ) at run time. The use case to provide condition value for an exclusive gateway following an user task completion .

I tried the following methods .

1. runtimeService.setVariable(processInstance.getId(),"variable","value");

2. Upon completing the task prior to the exclusive gateway
 
      taskService.complete(tasks.get(0).getId(), vars);

  (The conditional value for the gateway will be known only at this point in time , ie task completion, not prior to process instantiation)

Both are not working .

The only thing working is when i pass process variables as part of process instantiation.

How to accomplish this ? Any working examples / workarounds ?

Please help

Thanks
Niyas
11 REPLIES 11

hari
Star Contributor
Star Contributor
Hi,

If you use service task you can set the process variables there.
<code>
public void execute(DelegateExecution execution) throws Exception {
execution.setVariable("<VariableName>",<VariableValue>);
}
</code>

You can set them similarly in script task also.

niyas_m
Champ in-the-making
Champ in-the-making
Hi Hari,

  Thanks for the idea. Will try it

Thanks
Niyas

hari
Star Contributor
Star Contributor
I also think setting the variables map while completing a task should also work.

niyas_m
Champ in-the-making
Champ in-the-making
Hari,

  I tried this way as option # 2 mentioned in my original post. But unfortunately its not working . Values supplied as part of task completion is not carried over to the exclusive gateway for its conditional navigation.

Thanks
Niyas

jbarrez
Star Contributor
Star Contributor
> Both are not working .

What do yo mean? Those two option do that, they set the variables.
How did you verify that the variables were not set?

A small bit of test code would to understand what's happening here.

niyas_m
Champ in-the-making
Champ in-the-making
Hi Barrez,

  PFA the test code and the bpmn xml files for your review

Thanks
Niyas
 
 

niyas_m
Champ in-the-making
Champ in-the-making
Hi Barrez,

   The same process and test code is working when i use "sub process" instead of "call activity" . But ideally we will need to separate the business processes into different bpmn files . Please advise why the stuff that works with sub process is not working with call activity. Variable scope constraints ?.
Any solution to make it work with call activity ? .

Thanks
Niyas

jbarrez
Star Contributor
Star Contributor
Haven't looked at the xml but,

> The same process and test code is working when i use "sub process" instead of "call activity"

Call activity _do not_ inherit variables like subprocesses.
You need to explicitly map variables in and out for call activities (checks the docs on call activity)

niyas_m
Champ in-the-making
Champ in-the-making
Hi Barrez ,

   Thanks for the update.

   Yes , i have mapped the variables in call activity. ( sourceExpression="${action}" target="action" ) .
   Issue i am facing using call activity is , the value for the variable is reaching the X-OR gateway only when i declare them as part of process instance creation. (setting through run time service or task completion is not working) .

  But , i could do it through a service / script task .I am still wondering why the below methods are not working with call activity as it would have made the execution simple without service / script tasks elements

1. runtimeService.setVariable(processInstance.getId(),"variable","value");
2. taskService.complete(tasks.get(0).getId(), vars);

Thanks
Niyas