Create Task Listener - can't get name of task

In a process, I have defined a Task Listener to send a mail including the URL to the tasklist. This seemed to work fine, but today, the query to get the Task stopped working properly. I was using list() and .get(0) before which selected wrong names, probably I was just lucky that it worked in the past. However, I must be missing something obvious here. delegateTask.getId() returns the proper task ID, so why is my query to the TaskService failing and returning null instead of the task?

	public void notify(DelegateTask delegateTask) {
		String processName = delegateTask.getProcessEngine().getRepositoryService().getProcessDefinition(delegateTask.getProcessDefinitionId()).getName();
		Task task = delegateTask.getProcessEngine().getTaskService().createTaskQuery().taskDefinitionKey(delegateTask.getTaskDefinitionKey()).singleResult();
		String taskName = "";
		if(task != null)
		{
			taskName = task.getName();
			taskName.replace("#{Name}", delegateTask.getVariable("Name").toString()).replace("\n", "");
		}

Hi @mhoffmann,

it should be sufficient to work on the delegateTask.

Is there a reason to spend a hop to the database to get the same task?

Hope this helps, Ingo

1 Like

Nope, I was just utterly stupid I guess :sweat_smile:

I could swear that there wasn’t a getName() function on it the last time I tried. Probably my Eclipse either bugged or I just failed.

So the reason why I don’t get it via the DB query is that the task isn’t persisted to the database when the create task listener is called? Would make sense, since it isn’t called afterCreated.

Replacing it with a decent delegateTask.getName(); obviously did it.

@Ingo_Richtsmeier, when we configure TaskListener in the eventType=“create” , during execution the task listener code will be get executed before/after task creation?

I hope it would be after creation of the task entity, if yes, before/after persistence of task entity?

Hi @aravindhrs,

The task event cycle is reworked and clarified in the upcoming Camunda Release 7.12. The latest docs says:

The create event fires when the task has been created and all task properties are set. No other task-related event will be fired before the create event. The event allows us to inspect all properties of the task when we receive it in the create listener.

See Delegation Code | docs.camunda.org for the complete story.

Hope this helps, Ingo

Hi,

I also use a create TaskListener to get the name of a User Task with
String taskName = delegateTask.getName();

How can I do sth similar for a Service Task?
(since in Service tasks I cannot create a task listener and access the DelegateTask)

Thx

Edit: I think I found it: delegateExecution.getCurrentActivityName()