Set businessKey from start message event

Hi,

I am using Flowable 6.6.0
I need to set a businessKey for my processes.
My process can be started from two different ways:

1/ Programmatically through:

runtimeService.createProcessInstanceBuilder()
                .businessKey("myBusinessKey")

This way, I successfully set the businessKey.

2/ Through start message event
Here, I don’t know how to set the business key.

I tried:

runtimeService.updateBusinessKey(execution.getProcessInstanceId(), "myBusinessKey"));

but I can’t retrieve the process from the History service. I guess the process is already started and the key is not updated in the History service.

Is there a way to set the businessKey when the process starts from a “start message event” (Kafka) ?

Thanks!

Michel

Any help on this would be appreciated.

Hi Michel,

you can add a service task right after (Message start event) → (…) to set business key to the expected value.

Regards
Martin

Thanks Martin.

I set the businessKey in a service task right after the message start event.
However, when I use the historyService to get the historyProcessInstance , I am not getting any result.

historyService.createHistoricProcessInstanceQuery()
                    .processInstanceBusinessKey(businessKey)
                    .includeProcessVariables()
                    .singleResult();

The flow is synchronous from this service task. Is it the reason the historyProcessInstance is not updated with the business key?

By the way, I am querying the historyService with the business key in order to check the status of the process instance (running/done).

If history works (turned on, on the right history level, synchronous history…), it should work. If not could you try to reproduce the issue in the jUnit test?

Thanks @martin.grofcik
My mistake, it indeed works as expected

I found one issue in the case instance business key update. Case business key history update. · Issue #3087 · flowable/flowable-engine · GitHub It should not cause problems in your case because you are using 6.6.0

Martin

That is not a bug @martin.grofcik.
If CmmnRuntimeService#updateBusinessKey(String, String) is used, e.g. cmmnRuntimeService.updateBusinessKey(caseInstance.getId(), 'my key'), then everything should work correctly. Irregardless whether the async history is used or not.

Right I am using 6.6.0 but I am using BPMN and not case management

@mykeul if you use runtimeService.updateBusinessKey(execution.getProcessInstanceId(), "myBusinessKey")); then everything should be fine. The only thing is that if you try to query by the business key within the same transaction / command context execution then it won’t work.

Ideally you could provide a test case that we can look into.

It works now. It was a problem in my code.
I first thought that because I set the key after the wait state, it is not persisted yet and I cannot retrieve it from history service.
I understand that from the same transaction, it won’t work.

Thanks Filip!

From the backward compatibility point of view

    <case id="businessKeyUpdate">
        <extensionElements>
            <flowable:caseLifecycleListener targetState="active" expression="${caseInstance.setBusinessKey('updatedBusinessKey')}"/>
        </extensionElements>

performs businessKey update in the history too (version 6.6.1). Version 6.7.0 org.flowable.cmmn.engine.impl.history.DefaultCmmnHistoryManager#recordCaseInstanceStart is called before case instance business key initialization.

You are right, I can use cmmnRuntimeService.setBusinessKey(...) instead. I do not need to migrate definitions to higher versions.