# Set businessKey from start message event

**URL:** <https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457>\
**Category:** Flowable Engine\
**Created:** [September 26, 2021, 2:14pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457 "2021-09-26T14:14:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mykeul](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@mykeul](https://forum.flowable.org/u/mykeul)\
**Post date:** [September 26, 2021, 2:14pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/1 "2021-09-26T14:14:06Z")

</div>

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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![mykeul](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@mykeul](https://forum.flowable.org/u/mykeul)\
**Post date:** [October 10, 2021, 7:56pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/2 "2021-10-10T19:56:12Z")

</div>

Any help on this would be appreciated.

---

<div class="post-metadata">

**Author:** ![martin.grofcik](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/martin.grofcik/32/71_2.png) [@martin.grofcik](https://forum.flowable.org/u/martin.grofcik)\
**Post date:** [October 11, 2021, 11:50am UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/3 "2021-10-11T11:50:56Z")

</div>

Hi Michel,

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

Regards  
Martin

---

<div class="post-metadata">

**Author:** ![mykeul](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@mykeul](https://forum.flowable.org/u/mykeul)\
**Post date:** [October 11, 2021, 12:03pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/4 "2021-10-11T12:03:20Z")

</div>

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.

```auto
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).

---

<div class="post-metadata">

**Author:** ![martin.grofcik](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/martin.grofcik/32/71_2.png) [@martin.grofcik](https://forum.flowable.org/u/martin.grofcik)\
**Post date:** [October 11, 2021, 12:14pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/5 "2021-10-11T12:14:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mykeul](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@mykeul](https://forum.flowable.org/u/mykeul)\
**Post date:** [October 24, 2021, 7:37am UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/6 "2021-10-24T07:37:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martin.grofcik](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/martin.grofcik/32/71_2.png) [@martin.grofcik](https://forum.flowable.org/u/martin.grofcik)\
**Post date:** [October 25, 2021, 6:53am UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/7 "2021-10-25T06:53:35Z")

</div>

I found one issue in the case instance business key update. [Case business key history update. · Issue #3087 · flowable/flowable-engine · GitHub](https://github.com/flowable/flowable-engine/issues/3087) It should not cause problems in your case because you are using `6.6.0`

Martin

---

<div class="post-metadata">

**Author:** ![filiphr](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/filiphr/32/315_2.png) [@filiphr](https://forum.flowable.org/u/filiphr)\
**Post date:** [October 25, 2021, 12:25pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/8 "2021-10-25T12:25:59Z")

</div>

> [@martin.grofcik](#):
>
> I found one issue in the case instance business key update. [Case business key history update. · Issue #3087 · flowable/flowable-engine · GitHub](https://github.com/flowable/flowable-engine/issues/3087) It should not cause problems in your case because you are using `6.6.0`

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.

---

<div class="post-metadata">

**Author:** ![mykeul](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@mykeul](https://forum.flowable.org/u/mykeul)\
**Post date:** [October 25, 2021, 12:40pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/9 "2021-10-25T12:40:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![filiphr](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/filiphr/32/315_2.png) [@filiphr](https://forum.flowable.org/u/filiphr)\
**Post date:** [October 25, 2021, 12:44pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/10 "2021-10-25T12:44:41Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![mykeul](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@mykeul](https://forum.flowable.org/u/mykeul)\
**Post date:** [October 25, 2021, 12:52pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/11 "2021-10-25T12:52:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![martin.grofcik](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/martin.grofcik/32/71_2.png) [@martin.grofcik](https://forum.flowable.org/u/martin.grofcik)\
**Post date:** [October 25, 2021, 5:37pm UTC](https://forum.flowable.org/t/set-businesskey-from-start-message-event/8457/12 "2021-10-25T17:37:58Z")

</div>

From the backward compatibility point of view

```auto
    <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.
