Find the appropriate task ID automatically in Camunda

I am using Camunda in a Spring Boot application. How can I start or complete a task using the task ID automatically or find the appropriate task ID in Camunda .

Hello my friend!

To start (claim) or to complete a user task on camunda, first you need to find the userId from current user logged in.

        var userId = execution.getProcessEngineServices()
                .getIdentityService()
                .getCurrentAuthentication()
                .getUserId();

Then you can claim the task by passing the logged in userId, and the id of the task you want to claim/start. Using this code:

        execution.getProcessEngineServices()
                .getTaskService()
                .claim("yourTaskId", userId);

and to finish, you can complete the task by passing the taskId to the following code below:

        execution.getProcessEngineServices()
                .getTaskService()
                .complete("taskId");

NOTE:
If you need to programmatically fetch the taskId that your instance is at at that particular moment, you can use the code below:

        execution.getProcessEngineServices()
                .getTaskService()
                .createTaskQuery()
                .processInstanceId(execution.getProcessInstanceId())
                .active()
                .singleResult()
                .getId();

i hope this helps!

William Robert Alves