Adding candidate users from a tasklistener

I am trying to add candidate users from a rest call from an org chart.

The rest call returns an array with usernames and stores the call in a variable salesDepartment.

At create time of the task I want to run a javascript that loops the array and adds the users to the candidate users.

the code I’m trying this this:

<flowable:taskListener event="create" class="org.flowable.engine.impl.bpmn.listener.ScriptTaskListener">

var jsonstring = execution.getVariable("salesDepartment.responseBody")
var Sales = JSON.parse(jsonstring);

for (var i = 0, len = Sales.salesAdminUsers.length; i < len; i++) {
	  task.addCandidateuser(Sales.salesAdminUsers[i]);
}

I get the following error:

ReferenceError: "execution" is not defined in <eval> at line number 2

Anyone know how i can get processvariables in a ScriptTaskListener?

A task listener doesn’t have an execution, but a task. Swap the ‘execution’ with task and it should work in this script.