Start process instance by message and set business key

Hi,
I am back with my favorite messages.
I am trying to start processB from processA by startProcessInstanceByMessage(String messageName, String businessKey) link, because I want to set business key of processB.
When I try just the first parameter (messageName) the processB is started and no error is thrown, but when I try also the second parameter (businessKey) I get this exception:

The process could not be started. :
Cannot instantiate process definition Process_1:5:e1af2b32-0b2c-11e8-9ac4-605718bb85a1: Error while evaluating expression: ${ execution.getProcessEngineServices() .getRuntimeService() .startProcessInstanceByMessage(“MSG_START_1”,“bkOfSupportProcess”)}. Cause: Cannot coerce from class java.lang.String to interface java.util.Map

This is how it looks like.
image

Could you please help me with this?
I do not want to start processB and set up some correlationId variable, I want to use the businessKey, which I think should be used for the situation like this.

Thanks,
Lukas

Hi Lukas,

I think there are multiple methods with the signature RuntimeService#startProcessInstanceByMessage(arg1, arg2) and the EL runtime is not particularly clever in choosing the right one (=> doesn’t consider argument types). As workarounds, you can either implement a custom Spring or CDI bean that you call from the expression and that internally calls the correct Java API. Or your write the expression by using the message correlation builder API, via which you can express the same logic unambiguously.

Cheers,
Thorben

edit: Or implement the throwing event via a script or Java class.

1 Like

Hi Thorben,
I have been waiting for your response :slight_smile: I did the workaround using message correlation builder API in the meantime, but the thing is, that I can not set a process business key via the message correlation builder, or can I?

Now, I have something like this:

${
execution.getProcessEngineServices()
.getRuntimeService()
.createMessageCorrelation(“msg_start_newOrder_orderProcessing”) .setVariable(“correlationId”,execution.getBusinessKey())
.correlate()
}

Thanks for your response.
Cheers,
Lukas

1 Like

See MessageCorrelationBuilder#processInstanceBusinessKey. By the way, for timely responses we have a great support offering :wink:

1 Like

Hah, yea I know :slight_smile: about the support.
I did miss that you can set the business key with this method. All I saw was just:

Parameters:
businessKey - the businessKey to correlate on.

Thank you, I will try this.

1 Like

Hi,

I also want from one process to send a message to start a new process (with a message start event). But I want to pass/map the variables from the first process into the second.

How can I do this with the MessageCorrelationBuilder?
Thanks

Hi kontrag,
you can use this script

${
execution.getProcessEngineServices()
.getRuntimeService()
.createMessageCorrelation(“msg_start_newOrder_orderProcessing”)
.setVariable(“correlationId”,execution.getBusinessKey())
.correlate()
}

the exact line that you should be insterested in is
.setVariable(“correlationId”,execution.getBusinessKey())
where the first parameter is variable name and the second is variable value

Hope this helps.
Best regards
Lukas

Thanks @lhofer87.
However, this will give me only the businesskey and not all variables, right?
I wrote the following:

Map<String, Object> vars = execution.getVariables();
MessageCorrelationResult res = execution.getProcessEngineServices().getRuntimeService().createMessageCorrelation(“aMessageName”).setVariables(vars).correlateWithResult();

Seems to work.

Hi Kontrag,
no, it will set just the process variable. For the business key is different approach.
BR
Lukas

Hi
I have the same kind of process than lhofer87

and… reading your post, i tried something like

${execution.getProcessEngineServices().getRuntimeService().createMessageCorrelation(“myMessageNameFromProcessB”).processInstanceBusinessKey(“createThisBusinessKey”).correlateWithResult()}

the spec says

If the message is correlated to a message start event then the given business key is set on the created process instance.

That’sthe case
So i’m expecting to start by message my process B and associate it a new business key

But the result is

Cannot instantiate process definition ProcessA Error while evaluating expression: ${execution.getProcessEngineServices().getRuntimeService().createMessageCorrelation(“myMessageNameFromProcessB”).processInstanceBusinessKey(“createThisBusinessKey”).correlateWithResult()}. Cause: org.camunda.bpm.engine.ScriptEvaluationException: Unable to evaluate script: ReferenceError: “createThisBusinessKey” is not defined in <eval> at line number 1

Any help ?

1 Like

Hi Flo,
I think the only issue was the weird quotation marks wraping the createThisBusinessKey. So fix that and you will be fine.
Also, I have created some updated sample process for this, feel free to test it.
Glad to help you.
Cheers
Lukas

msgTest.bpmn (8.1 KB)

Hi Thank you so much!!
I tried your bpm and it works perfectly fine

But when i fix my Expression with yours (just changing the message name)

${execution.getProcessEngineServices().getRuntimeService().createMessageCorrelation(“myMessageNameFromProcessB”).setVariable(“correlationId”,execution.getProcessInstanceId()).processInstanceBusinessKey(‘Hi! I am your business key :)’).correlate()}.

i get

Unable to evaluate script: <eval>:1:2 Expected ; but found ! Hi! I am your business key :slight_smile: ^ in <eval> at line number 1 at column number 2

changing the business key to another value changes nothing

processInstanceBusinessKey(‘johndoe’)

Unable to evaluate script: ReferenceError: “johndoe” is not defined in <eval> at line number 1

i could take your bpm and adapt but i’d like to understand why it failed
Sorry …

Hi, could you please upload your bpmn file here so I can take a look at it?
We will find it, don’t worry :slightly_smiling_face:
L.

whatiswrong.bpmn (17.8 KB)

So,
it is a littbe bit hard for me to explain this, but I think the following happens:

  • you create a message correlation (everything is fine here)
  • process will sucessfully correlate and start a new process
  • because there is no transaction boudaries the process fails on the next activity (a service task with input parameter) and the error is not really transparent about what happened
  • the service task has one input parameter in a javascript:"${execution.processBusinessKey}" what this does is that the engine grab processBusinessKey and wrap it between the “${}” which will make a request for property with same name as a processBusinessKey, which will fire an error about undefined “value in business key”
  • I have changed the script to this java.lang.System.out.println("BK IS: " + execution.processBusinessKey); and everything works flawlessly :slight_smile:
    here is the dia: whatiswrong.bpmn (17.9 KB)

PS: if you want the previous version to be working, just create another variable in the correlation with the same name as the businessKey value so the request would look like this:
${execution.getProcessEngineServices().getRuntimeService().createMessageCorrelation("hereComeANewChallenger").setVariable("correlationId",execution.getProcessInstanceId()).setVariable("johndoe",execution.getProcessInstanceId()).processInstanceBusinessKey("johndoe").correlate()}
But I think this is wrong approach.
EDIT: I forgot the async after on the “start at receive” so if you will be working on that diagram, uncheck it so you see any potential errors.
Cheers
Lukas

Hi Kontrag

How did you implement this? Was it as end listener in javascript or an expression or what?. I cannot get this to work trying all sorts of different approaches.

I have the same use case as you where I am initiating a second workflow with message correlation and want to pass all the process variables from the first process into the second process.

Appreciate any help you or anyone else could offer.

regards

By the way, using this mechanism fails if a second message passes through the message send event expression, which is my use case. The error is the following:

${execution.getProcessEngineServices().getRuntimeService().createMessageCorrelation(“notificationsWorkflow1”).processInstanceBusinessKey(‘789’).correlate()}. Cause: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to jdk.nashorn.internal.runtime.ScriptObject

thanks a lot my friend, do you can pass all variables to the other process?