Dynamically setting FormKey based on Process Instance and Task Id

Greetings,

we are currently developing an external task list and we want to generate custom URLs based on process and task data.
For example:

Login | HSTS Redirection Community

where {taskId} and {processId} are set through expressions inside the Camunda Modeler, and taken form process variables (process ID is an external ID from our database that we set in the Camunda process when starting it).

I want to be able to set the {taskId} dynamically for each task generated, I already have this task id when querying the Get Tasks REST method, as the “id” variable, but I am unable to set this id as part of the formKey through Camunda Modeler.

Is it possible to create a listener or input/output combination to take the Task ID from the process instance (not the Task Id in the modeler definition) that assigns the task Id to the Form Key for each unique task instance?

The end result would be something like:

User A:

/process/b493df5f-267d-11ec-9c92-0a93d3b15900/4152/taskType

User B:

/process/68e976da-2681-11ec-9c92-0a93d3b15900/4152/taskType

Currently it arrives like this, since the taskId variable is empty

/process//4152/taskType

While possible to set the taskId variable to something like “placeholder” and then in the external task list do a find and replace of the placeholder for the task id, it would be ideal to be able to generate the formKey with the full URL inside the Camunda process so that any time the API is called we have the URL available without needing extra steps outside of Camunda itself.

Hi @EPV,

the Task ID from the modeler can be referred as delegateTask.getTaskDefinitionKey() in a Task listener.

Here you can access the Task ID as delegateTask.getId() as well.

Hope this helps, Ingo

Thank you for the reply @Ingo_Richtsmeier, I’m struggling a litle bit with this since I’m not yet familiar with Java.

Should I be able to access the delegateTask.getDefinitionKey() as a Java class in a Groovy script like this?

Hi @EPV,

if you provide the Task Listener as a script, you can reference the task directly with task: Scripting | docs.camunda.org

I haven’t used Groovy by myself, but I guess that the methods of the task object are available as well: DelegateTask (Camunda Platform Javadocs 7.15.6-ee)

Hope this helps, Ingo

If I’m understanding this correctly it means that I can set a Task Listener as event type create with a script such as

execution.setVariable(‘taskId’, task.id);

And I could reference that task.id
I think I am going in the right direction, I have been able to set the ${taskId} variable in the formKey through the Input/Output parameters as such:

Obviously this sets it to “task.id” and not the actual Task Id, I have tried to use task.id, task.getId() and delegateTask.getId() but all are incorrect, as I get a “no such property exists” error

Cannot instantiate process definition Process_0d59h8j:97:4f00dcfa-2756-11ec-9c92-0a93d3b15900: Unable to evaluate script: groovy.lang.MissingPropertyException: No such property: task for class: Script74
Possible solutions: class [ start-instance-error ]

Hi @EPV,

as an input mapping can be applied to all BPMN tasks, the variable task, which refers to a User Task object, is not defined.

But a task listener would do the same as an input mapping, if you set the variable with

execution.setVariableLocal("varName", "value");

If you want to stick to input mapping, be aware that the return value of the script will be mapped into the Local Variable named in the first field.

Hope this helps, Ingo

Thanks again for the reply @Ingo_Richtsmeier I’m sure I’m just missing something very obvious here.

My issue is that I can’t figure out how to assign the value to the variable, whether through a Listener or an Input mapping using delegateTask.getId() as the value to assign to the variable returns that there is no property for the class.

For example, using a Listener of Event Type create

execution.setVariableLocal(‘taskId’, delegateTask.getId());

And this is the error output

Cannot instantiate process definition Process_0d59h8j:135:b3e5b011-2764-11ec-9c92-0a93d3b15900: ENGINE-03051 There was an exception while invoking the TaskListener. Message: ‘Unable to evaluate script while executing activity ‘Activity_08rbaai’ in the process definition with id ‘Process_0d59h8j:135:b3e5b011-2764-11ec-9c92-0a93d3b15900’: groovy.lang.MissingPropertyException: No such property: execution for class: Script95’ [ start-instance-error ]

Is this because I am trying to call something that’s out of scope? Or is this a configuration issue on my end?

I’m coming back to this thread to post the solution I arrived to a few days ago:

Using a task listener on event create, with a javascript inline script I assigned the Id value to a variable as follows

task.execution.setVariable(‘varTaskId’, task.execution.getId());

It was a scope probem, task.execution was the correct to use, not delegateTask.
Thanks again for the help.

Correction, to get the task’s id itself the correct form is:

task.execution.setVariable(‘varTaskId’, task.getId());

To get the process Id:

task.execution.setVariable(‘varTaskId’, task.execution.getId());