Retrieving Process Variables

Hello Camunda Community,

Hi,
While reading (https://docs.camunda.org/manual/7.5/user-guide/process-engine/variables/) I am not sure how you retrieve a variable.?

At the moment I am struggling to find out how to access previously set process variable. What I tried is:

I have a simple bpmn process in which i have start event,1 service task and end event,i am starting my process by passing 2 variables (a&b) and my service task is implementing following java class:

‘’’
public class Addition implements JavaDelegate {

public void execute(DelegateExecution exe) throws Exception {
	
	System.out.println("Inside calculator again");

    Integer x = (Integer) exe.getVariable("a");
    Integer y = (Integer) exe.getVariable("b");
    int add = x+y;
    System.out.println("Addition of two number is"+add);
    exe.setVariable("add",add);

}

I am starting my process as follows:

‘’’
public void sayHello(ProcessEngine processEngine)
{
Map<String,Object> variables = new HashMap<String, Object>();
variables.put(“a”, 3);
variables.put(“b”, 5);
ProcessInstance instance= processEngine.getRuntimeService().startProcessInstanceByKey(“Process_3”, variables);

}

‘’’

I want to access ‘add’ variable(present in Addition class) in sayHello class?
As process has been completed so i can’t use runtimeService so I tried to use history service but couldn’t find out any solution.
Is there any java api which i can use or is there any other way??

Any help is appreciated.

Thank you
Keshav Taparia

I’ve never used the java api, but is it possible that you cannot retrieve the runtime variables because the execution has no pauses and you are trying to access an execution that has already finished?

Cheers, Gonzalo.

1 Like

Dear gcalvo,

Thanks for your prompt reply.Yes that’s correct i can’t use runtime service for variables.

Can i use some historic query or anything else so that i can use my process variable after completion of my process?

Thanking you,
Keshav taparia

You can do it with rest, so i guess you can do it in java also.

But as I said, I dont work with java, so I can’t help you there.

Good luck!

Gonzalo

1 Like

Dear gcalvo,

Thanks for your reply.I don’t know much about how to use REST API here so can you suggest me something in what way i should proceed to use REST.

Is there any example which i can look in?

Thankyou
Keshav Taparia

https://docs.camunda.org/manual/7.4/reference/rest/history/variable-instance/get-variable-instance-query/

Plenty of examples in the documentation.

1 Like

Hi Keshav,

You can find java examples here

There are lot more examples in the mentioned link.

Hope this helps.

Cheers,
Deivarayan

1 Like

Hello Deivarayan,

I truly appreciate your time and effort. You saved my time and project.It helped.

Thanks a lot,

Keshav Taparia

Hi keshav kumar taparia,

sayHello is the launcher of the process & where you want to get the output of your process (if I understand you well).
Actually I don’t see your scenario as an acceptable use case to be implemented as a BPMN model.

A series of activities will be executed following the start event
In your case all subsequent activities are automated (service task & end event)
but in most use cases many of subsequent activities require user interaction or get in a wait state.

Even in your case it needs unknown time for the automated activities to be done

You can attach an ExecutionListener to the end event to get the output at the proper time
See below link
https://docs.camunda.org/manual/7.5/user-guide/process-engine/delegation-code/#execution-listener

1 Like

Hi Hassang,

Actually I am new to camunda- bpm and exploring it that’s why i am doing nothing in my process.

I added a receive task before my end event and then by using following query i am able to get access to my add variable in sayHello class.
processEngine.getRuntimeService().getVariable(instance.getId(), “variable”)

and then i can end my process by using
Execution execution = processEngine.getRuntimeService().createExecutionQuery()
.processInstanceId(instance.getId()).activityId(“ReceiveTask_1vxlm60”) .singleResult();
processEngine.getRuntimeService().signal(execution.getId());

Alternatively i can use HistoryService query after completion of my project to get ‘add’ variable in sayHello class as suggested by Deivarayan.

Thank you,
Keshav Kumar Taparia

Dear Hassang Sir,

I tried to attach an ExecutionListener to the end event as suggested by you,and it is implementing ExampleExecutionListner as follows:

‘’’
public class ExampleExecutionListener implements ExecutionListener {

public void notify(DelegateExecution execution) throws Exception {
	execution.setVariable("c", execution.getVariable("add"));
}

}
‘’’

I am setting my c in Calculator class which is implementing by my service task as follows:

‘’’
public class Calculator implements JavaDelegate {

public void execute(DelegateExecution exe) throws Exception {
	
	System.out.println("Inside calculator again");

    Integer x = (Integer) exe.getVariable("a");
    Integer y = (Integer) exe.getVariable("b");
    int add = x+y;
	System.out.println("Addition is"+add);
	exe.setVariable("c", add);
    
}

}

Now In my sayHello class i am trying to retrieve my “c” variable as follows:

‘’’
ProcessInstance instance= processEngine.getRuntimeService().startProcessInstanceByKey(“Process_2”, variables);
Object varSetByListener = runtimeService.getVariable(instance.getId(), “c”);
variables.put(“c”,varSetByListener);
‘’’

It is throwing NullPointer Exception at(Object varSetByListener = runtimeService.getVariable(instance.getId(), “c”).

FYI It is going inside my ExampleExecutionListener class and after that it is throwing Exception.
Can you please help me out here??

I also want to know when will my process will be completed?

Any help is appreciated.
Awaiting your reply
Thanking you,
Keshav Kumar Taparia

Hi Keshav,

In your case, you can only access the runtime variable inside the execution listener. As far as i understand, you dont have any activity where the execution would wait, so process ends after startProcessInstanceByKey() is executed. So you cannot access the runtime variable after the process has ended.

Also I dont get a clear picture of your BPMN model. Could you post your bpmn diagram/xml?

Cheers,
Deivarayan

Hi

My solution is valid in case you want to get the output at the proper time but not from the process launcher code which is sayHello in your case.

I assumed it is OK for you to read the output from the ExecutionListener which is ExampleExecutionListener in your case.

In case you want to read it from the launcher code then you can try Webcyberrob’s solution below

OR

thorben’s solution in case you have camunda version 7.6.0-alpha2 or above

1 Like

Dear Deivaryanaz,

Thanks for your prompt reply. PFA my process bpmn file.

add_mul.bpmn (3.5 KB)

Here is my sayHello class implementation-

‘’’
public void sayHello(ProcessEngine processEngine) {
Map<String,Object> variables = new HashMap<String, Object>();
try {
System.out.println("inside postdeploy ");
variables.put(“a”, 2);
variables.put(“b”, 5);
variables.put(“c”, 0);

ProcessInstance instance=  processEngine.getRuntimeService().startProcessInstanceByKey("Process_2", variables);
Object varSetByListener = runtimeService.getVariable(instance.getId(), "c");
Integer x = (Integer)varSetByListener;
variables.put("c",varSetByListener);

‘’’

I am passing variables as an argument where i pass a and b as process variable and i use in my calculator class and want to retrieve their addition in variable c in my sayHello class.

I hope now you understand my problem.

Awaiting your reply,

Thanking you,
Keshav Taparia

1 Like

Hi Keshav,

One additional note.

Starting with 7.6.0-alpha2, there is an API method for your use case, see here

Hope this helps

Cheers,
Deivarayan

1 Like