How to get process variable inside a task listener?

Hello all,

I have a user task and a task listener (event: complete) implemented by a java class.

public class user implements TaskListener {

	  public void notify(DelegateTask delegateTask) {
	     
	        //how can I get the process variable "oldCustomer (Boolean)" ??

	  
	        // based on process variable value, a BPMN error is thrown
	  
		  if( ... ) {
			  throw new BpmnError("NotFound");
		  } 
	  }

}

I would like to know how can I fetch process variables inside the task listener.

My scenario is the following:
I have a user task, where an employee can fill out a form for this task. If the employee checks a checkbox “Customer not found”, a process variable “oldCustomer (Boolean)” is created. Then, before the process execution goes to the next task, I have implemented a task listener (event: complete, implemented by java class), in which I want to fetch the process variable “oldCustomer” that is created from the form submission and based on its value to throw or not a BPMN error.

My questions are the following:

  1. Is the process variable “oldCustomer” created once the user submits the form or when the user task has been successfully completed?
  2. If the process variable is created once the user submits the form, how could I fetch this variable from a task listener?

Thank you in advance.

Once the user submits the form, you should have access to the process variable in your complete listener. You should be able to grab your process variable via

delegateTask.getVariable("oldCustomer")
2 Likes

Hi @jgigliotti,

thanks for your answer.

I try to do the following in my java class but I receive an error “Type mismatch cannot convert Object to Boolean”.

public void notify(DelegateTask delegateTask) {
		    // Custom logic goes here
			System.out.println(delegateTask.getVariable("oldCustomer"));;
			
			Boolean appr=delegateTask.getVariable("oldCustomer");
			if(appr==false) {
				throw new BpmnError("Rejected");  //new customer, then throw an error
			}
			
		}

What do I need to change?
Thank you in advance for your help.

getVariable returns an Object. You should be able to cast it to Boolean

Boolean appr = (Boolean) delegateTask.getVariable("oldCustomer");
2 Likes