Process variable in Service task

Dear All,

I have a simple bpmn processsample.bpmn (3.6 KB)
in which i am using 2 service task,I am executing my process by using
processEngine.getRuntimeService().startProcessInstanceByKey(“Process_1”, variables);
where my variables is as follows:

Map variables = new HashMap();
variables.put(“a”, 2);
variables.put(“b”, 5);

My first service task is implementing Addition java class and my second service task is implementing multiplication class.

Now I want to have 3 variables(constants) “c”,“d”,and “e” such that c=5,d=10,e=2 so that i can use my “c” variable in for service task 1 such that in addition class i can use this variable,similarly i want to use variable “d” in my multiplication class and variable “e” i want to define globally so that i can use this in both class.

Can anyone guide me on this?

Any help is appreciated :slight_smile:

Thanking you,
Keshav Taparia

1 Like

Hi @k.taparia,

When you implement a service task in Java, you typically implement the interface org.camunda.bpm.engine.delegate.JavaDelegate (see https://docs.camunda.org/manual/7.5/reference/bpmn20/tasks/service-task/#calling-java-code). In its #execute method, you are handed a org.camunda.bpm.engine.delegate.DelegateExecution object that has get and set methods for variables (for example DelegateExecution#getVariable, see https://docs.camunda.org/manual/7.5/user-guide/process-engine/variables/#set-and-retrieve-variables-overview). That way, you can access any variable in the process context.

Now, if your service task needs variable c, you have to set it beforehand, for example when you start the process (where you currently set variables a and b). If c is indeed a constant, then I suggest you use a Java static field for it instead of a process variable.

I hope that helps.

Cheers,
Thorben

1 Like

Dear Thorben Sir,

I truly appreciate your time and effort.I was thinking of defining these variables in my camunda modeler(i.e inside process model itself)

In camunda modeler with service task we can attach some input/output

and in extension we can have some properties also.My question is can we define our process variables there,if not then where and how can we use them.

Thanking you,
Keshav Taparia