How to get the Camunda Task Assignee FirstName and LastName

Hi Camunda Team,
We are trying to get the task assignee first name and last name from camunda BPMN file. As part of the BPMN file we are adding task listeners and using assignment as script and event type as assignment and listener type as script and format as java script with type inline script.
Below is the BPMN diagram.

task.setVariable(“claimedBy”,task.getAssignee())

Here we are getting the task.getAssignee() which is returning userId, I need to get the Assignee first name and last name.

Below are the user/assignee details there in h2 database.

Before claiming the task the task will be looks like below.

After claiming the task we are not getting the task assignee firstname and lastname.


Below is the response from the task API from camunda UI.

{
    "_links": {
        "assignee": {
            "href": "/user/user"
        },
        "execution": {
            "href": "/execution/7a8b1d4e-ee01-11ef-96cc-103d1ca05358"
        },
        "identityLink": {
            "href": "/task/7a8b1d51-ee01-11ef-96cc-103d1ca05358/identity-links"
        },
        "processDefinition": {
            "href": "/process-definition/camunda4ic:1:28c38e71-ee00-11ef-96cc-103d1ca05358"
        },
        "processInstance": {
            "href": "/process-instance/7a8af63b-ee01-11ef-96cc-103d1ca05358"
        },
        "self": {
            "href": "/task/7a8b1d51-ee01-11ef-96cc-103d1ca05358"
        }
    },
    "_embedded": {
        "identityLink": [
            {
                "_links": {
                    "task": {
                        "href": "/task/7a8b1d51-ee01-11ef-96cc-103d1ca05358"
                    },
                    "user": {
                        "href": "/user/user"
                    }
                },
                "_embedded": null,
                "type": "assignee",
                "userId": "user",
                "groupId": null,
                "taskId": "7a8b1d51-ee01-11ef-96cc-103d1ca05358"
            }
        ],
        "processDefinition": [
            {
                "_links": {
                    "deployment": {
                        "href": "/deployment/28c207cf-ee00-11ef-96cc-103d1ca05358"
                    },
                    "resource": {
                        "href": "/deployment/28c207cf-ee00-11ef-96cc-103d1ca05358/resources/camunda4ic.bpmn"
                    },
                    "self": {
                        "href": "/process-definition/camunda4ic:1:28c38e71-ee00-11ef-96cc-103d1ca05358"
                    }
                },
                "_embedded": null,
                "id": "camunda4ic:1:28c38e71-ee00-11ef-96cc-103d1ca05358",
                "key": "camunda4ic",
                "category": "http://bpmn.io/schema/bpmn",
                "description": null,
                "name": "Camunda4IC Task",
                "versionTag": null,
                "version": 1,
                "deploymentId": "28c207cf-ee00-11ef-96cc-103d1ca05358",
                "diagram": null,
                "suspended": false,
                "contextPath": null
            }
        ],
        "user": [
            {
                "_links": {
                    "self": {
                        "href": "/user/user"
                    }
                },
                "_embedded": null,
                "id": "user",
                *"firstName": "Mr",*
*                "lastName": "UserA20",*
                "email": "user@localhost"
            }
        ]
    },
    "id": "7a8b1d51-ee01-11ef-96cc-103d1ca05358",
    "name": "Camunda 4IC",
    "assignee": "user",
    "created": "2025-02-19T01:05:59.009+1100",
    "due": null,
    "followUp": null,
    "delegationState": null,
    "description": null,
    "executionId": "7a8b1d4e-ee01-11ef-96cc-103d1ca05358",
    "owner": null,
    "parentTaskId": null,
    "priority": 50,
    "processDefinitionId": "camunda4ic:1:28c38e71-ee00-11ef-96cc-103d1ca05358",
    "processInstanceId": "7a8af63b-ee01-11ef-96cc-103d1ca05358",
    "taskDefinitionKey": "FourICUserTask",
    "caseExecutionId": null,
    "caseInstanceId": null,
    "caseDefinitionId": null,
    "suspended": false,
    "formKey": null,
    "camundaFormRef": null,
    "tenantId": null
}

Is there any way to get the task assignee firstname and lastname instead of userId.

Below is the BPMN diagram we are using.
camunda4ic.bpmn (4.8 KB)

Thanks,
Ram

Hey @Ramanaiah, have you tried querying the Camunda identity service in your script using the userId as a filter?

The query should return a “user” object from which you can extract first name and last name.

2 Likes

@herrier do you have any code snippets for this?

Hi @Ramanaiah, I tried this in your BPMN and it retrieves the first name and last name from the assignee …

camunda4ic (1).bpmn (5.2 KB)

import org.camunda.bpm.engine.IdentityService

task.setVariable("claimedBy",task.getAssignee())

IdentityService identityService = task.getExecution().getProcessEngineServices().getIdentityService()

String assignee = task.getAssignee()
def user = identityService.createUserQuery().userId(assignee).singleResult()
task.setVariable("assigneeFirstName", user.getFirstName())
task.setVariable("assigneeLastName", user.getLastName())
2 Likes

@herrier thank you this is worked.
Can we do the same in javascript, as we not using groovy.

Yes you can, change the “Format” of your listener to Javascript and ask ChatGPT to convert the Groovy to Javascript.

1 Like

This worked for me.

const identityService = task.getExecution().getProcessEngineServices().getIdentityService();
		const assignee = task.getAssignee();
		const user = identityService.createUserQuery().userId(assignee).singleResult();
		task.setVariable("claimedBy", user.getFirstName() + ' '+user.getLastName());

Thanks alot @herrier

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.