cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrieve business key from task delegate

yangyang_qian
Champ in-the-making
Champ in-the-making
Our web app currently starts a process with a business key …

String processName = getProcessName();
String businessKey = getBusinessKey();
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runTimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runTimeService.startProcessInstanceByKey(processName, businessKey);
the process runs and then waits at the first user task to be externally signalled via a TaskListener attached to the User Task … so in my

public class GenericHumanTaskListener implements TaskListener {
     public void notify (DelegateTask delegateTask) { … }
}
function I am trying to retrieve the business key that the process started with through the delegateTask object … is there a way to do so?

I guess the alternative would be to start the process with a variables HashMap … but I was hoping the business key would be retrievable without having to restore to putting it in the variables map.
5 REPLIES 5

dankre
Champ in-the-making
Champ in-the-making
Hi,

I have not tested it. Propably you can give it a try. You have to inject an instance of your runtimeService to your Tasklistener.


ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
ProcessInstance result = processInstanceQuery.processInstanceId(delegateExecution.getProcessInstanceId()).singleResult();
String businessKey = result.getBusinessKey();

Regards,
Daniel

frederikherema1
Star Contributor
Star Contributor
That's not a good idea… You shouldn't use activiti API from within TaskListeners (nor from ExecutionListeners or JavaDelegates), this can mess up transactions.

The only way I can think of is casting the DelegateExecution (delegateTask.getExecution()) to the Impl and going all the way up to the root execution (which is the process-instance) and get the business-key from there.

yangyang_qian
Champ in-the-making
Champ in-the-making
Ah ok, didn't realize there wasn't a built-in way to grab the business key from the DelegateTask …

I think to play it safe I'll just stick with putting the business key in the variables map …



public class GenericRequest {


private void startWorkflow() {
  String processName = determineProcessNameFromFields();
  ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  RuntimeService runTimeService = processEngine.getRuntimeService();
  ProcessInstance processInstance = runTimeService.startProcessInstanceByKey(
            processName,
            this.businessKey,
            CollectionUtil.singletonMap(
              "RequisitionId",
              this.businessKey
            )
           );
}


}

and just retrieve it using getVariable


public class GenericHumanTaskListener implements TaskListener {



@Override
public void notify (DelegateTask delegateTask) {
  String wfeTaskID = delegateTask.getId();
  String taskName = delegateTask.getName();
  String businessKey = delegateTask.getVariable("RequisitionId").toString();
                …
 
}



}


Thanks for the suggestions!

frederikherema1
Star Contributor
Star Contributor
Yes, that's the safest. Maybe you can file an issue in our JIRA, because it should be easier to get business-key for a certain task/delegateTask

yangyang_qian
Champ in-the-making
Champ in-the-making