# How to get unique ID of current serviceTask execution in java delegate class?

**URL:** <https://forum.flowable.org/t/how-to-get-unique-id-of-current-servicetask-execution-in-java-delegate-class/10864>\
**Category:** Flowable Engine\
**Created:** [September 21, 2023, 7:31pm UTC](https://forum.flowable.org/t/how-to-get-unique-id-of-current-servicetask-execution-in-java-delegate-class/10864 "2023-09-21T19:31:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmarekk11](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@mmarekk11](https://forum.flowable.org/u/mmarekk11)\
**Post date:** [September 21, 2023, 7:31pm UTC](https://forum.flowable.org/t/how-to-get-unique-id-of-current-servicetask-execution-in-java-delegate-class/10864/1 "2023-09-21T19:31:00Z")

</div>

Hello,  
is there a way how to get unique id of current serviceTask execution in java execute method (my class implements JavaDelegate interface)? I spent a lot of time to find it but unsucessfuly.

Reason why I need it is, that I will have a loop and inside it serviceTask will be executed. I would like to get unique ID of this serviceTask and use it in a map (flow variable) where it will be as a key. In value I will store other data related to this particular serviceTask execution.

There is a table where this ID is present. Table name is “ACT\_HI\_ACTINST” and column I need is “ID\_”.

 ![obrázok](https://canada1.discourse-cdn.com/flex035/uploads/flowable/original/2X/8/87e72e218b0429ec7a047e7b7e84bee9df3b2a43.jpeg)  
However I am not able to get it in the current execution method of my delegate class.  
I am able to get ids of previous finished activities by calling:

`runtimeService.createActivityInstanceQuery().processInstanceId(execution.getProcessInstanceId()).list();`

This will return list of ActivityInstanceEntity objects and their id is what I am looking for. But there are only finished acitivities.

I tried to use this query above in onEnd listener, but serviceTask the listener is attached to is still not there.

During debugging I went through whole DelegateExecution object which is input param into execution method, but I could not find it there. There is for example “EXECUTION\_ID\_” from above table, but it is not unique as you can see. During debug (when stopped on a breakpoint) I also checked tables “ACT\_RU\_ACTINST” and “ACT\_HI\_ACTINST”, but record for currently executed serviceTask is not yet there.

Is there any way how can I obtain it? If not do you have an idea how can I store data related to each particular serviceTask execution? But I need to know to which one exactly it belongs to. Because I am sending then this info to FE to render it.  
For this reason I use:

`historyService.createHistoricActivityInstanceQuery().processInstanceId(process.getId()).activityTypes(Sets.newHashSet("userTask","serviceTask")).list()`

And these data should be extended by my custom info.

Something like this I will use for defining serviceTask:

```auto
<serviceTask id="createCards" name="Create Cards" flowable:delegateExpression="${addCardDelegate}">

```

Thanks.

---

<div class="post-metadata">

**Author:** ![MeAndMyTeam](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/meandmyteam/32/3734_2.png) [@MeAndMyTeam](https://forum.flowable.org/u/MeAndMyTeam)\
**Post date:** [August 11, 2025, 5:29am UTC](https://forum.flowable.org/t/how-to-get-unique-id-of-current-servicetask-execution-in-java-delegate-class/10864/2 "2025-08-11T05:29:36Z")

</div>

```auto
public void execute(DelegateExecution execution) {
    CommandContext commandContext = Context.getCommandContext();
    if (commandContext != null) {
        ExecutionEntity executionEntity = (ExecutionEntity) execution;

        ActivityInstanceEntityManager activityInstanceEntityManager =
                CommandContextUtil.getActivityInstanceEntityManager(commandContext);

        ActivityInstanceEntity unfinishedActivity =
                activityInstanceEntityManager.findUnfinishedActivityInstance(executionEntity);

```

Maybe the method findUnfinishedActivityInstance can help you?

---

<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:** [August 18, 2025, 11:56am UTC](https://forum.flowable.org/t/how-to-get-unique-id-of-current-servicetask-execution-in-java-delegate-class/10864/3 "2025-08-18T11:56:01Z")

</div>

```auto
public void execute(DelegateExecution execution) {
    execution.getId();
}

```

---

<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:** [August 18, 2025, 11:57am UTC](https://forum.flowable.org/t/how-to-get-unique-id-of-current-servicetask-execution-in-java-delegate-class/10864/4 "2025-08-18T11:57:45Z")

</div>

That’s not correct @martin.grofcik. The execution is being reused. So if you have 2 service task after each other, both of them will have the same `DelegateExecution`.

---

<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:** [August 18, 2025, 12:00pm UTC](https://forum.flowable.org/t/how-to-get-unique-id-of-current-servicetask-execution-in-java-delegate-class/10864/5 "2025-08-18T12:00:10Z")

</div>

> [@mmarekk11](#):
>
> ACT\_HI\_ACTINST

My fault → ACT\_HI\_ACTINST id.

(JavaDelegate can be used in sequence flows….)

> [@mmarekk11](#):
>
> If not do you have an idea how can I store data related to each particular serviceTask execution? But I need to know to which one exactly it belongs to. Because I am sending then this info to FE to render it.  
> For this reason I use:

Why do you need to store data for the service task? Is it better to use userTask local variables?
