Complete a user task in its task listener

Hi,
I want to complete a user task in its task listener. For this purpose, I am simply getting the id of the task in its task listener and then call a rest API to complete the task.
Although the task id is correct, I encounter the following error when calling the rest API:

“org.camunda.bpm.engine.rest.exception.RestException: Cannot complete task <task-id>: Cannot find task with id <task-id>: task is null”

My code is shown below:

@Override
    public void notify(DelegateTask delegateTask) {
        var taskId = delegateTask.getId();
        try {
            completeUserTask(taskId);
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
            throw new RuntimeException(e);
        }
    }

    private void completeUserTask(String taskId) throws IOException, InterruptedException {
        var completeUserTaskRequest = HttpRequest.newBuilder(URI.create(baseUrl + "/task/" + taskId + "/complete"))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(""))
                .build();
        var completeUserTaskResponse = HttpClient.newHttpClient().send(completeUserTaskRequest, HttpResponse.BodyHandlers.ofString());
        System.out.println(completeUserTaskResponse);
    }

And this is my task listener:
image

Could anyone guide me what the problem could be?
I appreciate any help.

Hi @NSh ,

if I saw correctly, you assigned the task listener to the “create” event. The task listener, then tries to complete itself within creation lifecycle state. At this time, the task is not yet persisted to the data base, thus it can not be found. Committing to the data base happens after processing all listener assigned to “create” state. If you really want to finish this task automatically, you could use DelegateTask#complete method directly.

I hope this helps,
Adagatiya

1 Like

Hi @Adagatiya ,
Thanks a lot. That was exactly what I needed.

1 Like