How to access input/output parameters in Java

Hello everyone,

How to access input/output parameters set in ServiceTask in Camunda-Modeler using Java?.

Is it just using execution.getVariable(variableName);

Best Regards,
Raghu

That should work - but filling in the input/output parameters isn’t a requirement, you can get any variable this way provided that it was set into the contenxt

Hi Niall,
Thank you for responding.
I dont see anything in the logs.

I set the input parameter “oid” to a string value as shown in the picture below

and my code is

public class ServiceTask implements JavaDelegate{

@Override
public void execute(DelegateExecution execution) throws Exception {
	// TODO Auto-generated method stub
	System.out.println("In Service Task");
	long x;
	x = 5;
	if ((x % 2) == 0) {
		System.out.println("Number is even");
		execution.setVariable("x", "Number is even");
	}
	else {
		System.out.println("Number is odd");
		execution.setVariable("x", "Number is odd");
	}
	Object obj = execution.getVariable("oid");
	System.out.println("value is " + obj.toString());
}

}

I deployed the .war file onto Tomcat and started the process in camunda tasklist. But in the logs, I dont see the value of “oid”. Can you help me out?.

Best Regards,
Raghu

Raghu, try this:

@Service("Logger")
public class LoggerDelegate implements JavaDelegate {

    final Logger LOG = LoggerFactory.getLogger(LoggerDelegate.class);

    public void execute(DelegateExecution execution) throws Exception {

        LOG.info("\n\n  ... LoggerDelegate invoked by "
                + "processDefinitionId=" + execution.getProcessDefinitionId()
                + ", activityId=" + execution.getCurrentActivityId()
                + ", activityName='" + execution.getCurrentActivityName().replaceAll("\n", " ") + "'"
                + ", processInstanceId=" + execution.getProcessInstanceId()
                + ", businessKey=" + execution.getProcessBusinessKey()
                + ", executionId=" + execution.getId()
                + ", modelName=" + execution.getBpmnModelInstance().getModel().getModelName()
                + ", elementId" + execution.getBpmnModelElementInstance().getId()
                + " \n\n");

        LOG.info("--- Variables ---");
        Map<String, Object> variables = execution.getVariables();
        for (Map.Entry<String, Object> entry : variables.entrySet())
            LOG.info(entry.getKey() + " : " + entry.getValue());
    }
}

or

execution.getVariables().forEach((key, value) -> log.info("{} : {}", key, value));

1 Like

Hey Rob,

Thank you for the reply. Unfortunately, it is the same, I dont see anything in the logs. Is there anything else I could do?.

Best Regards,
Raghu

Raghu, where do you expect this oid variable to come from? Did you set this variable before or are you just trying to get the id of the process instance? That would be: execution.getProcessInstanceId()
If you set a process variable “oid” of type String before then the code
execution.getVariables().forEach((key, value) -> log.info("{} : {}", key, value));
would show this variable.
Remove the mapping. You don’t need to map explicitly.

I set the inputParameter “oid” for the ServiceTask in the modeler to a random value. And I am not trying to get processInstanceId. I am trying to access the value set for the variable “oid” in my code snippet posted in the original question. Can you look at the picture I attached in the question?. Can we just forget about the logs for now. Sorry for the confusion.

Best Regards,
Raghu

Raghu, hard to tell what your issue is without the process model, but here is a working example. It
a) maps a static string “oid” into a script task and prints it
b) maps a static string “oid” into a service task (delegate expression to Spring bean), which prints it

Please compare your model to this one. If you find the issue please share it. if you don’t, please share the process model.

Are you able to check the values in the cockpit? http://localhost:8080/camunda/app/cockpit/default/#/dashboard
Either for x or oid for the process Instance?