I start a process inside a Activiti Java Delegate. After the process is started, I try to query the first task and set some properties/variables on the task. The problem is, that the task query returns null. The task is only null if I execute the code within a delegate. If I do the same thing outside the delegate, everything works fine.
public class MyDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
ProcessInstance instance = execution.getEngineServices()
.getRuntimeService()
.startProcessInstanceById("processId"); // instance is created and has an id
Task task = execution.getEngineServices()
.getTaskService()
.createTaskQuery()
.processInstanceId(instance.getProcessInstanceId())
.singleResult(); // returns null
// set dueDate, assignee, variablesLocal ...
}
}
Activiti Version: 5.19.0.3
Is the task defined as synchronous or asynchronous? I ask because if it is async the creation of the first task will be itself delegated to the job executor. Meaning it may not be available immediately you start the process. Remember, BPM systems are not real time systems (this is a fact for ALL BPM systems).
If the task is synchronous, then it should be executed on the same thread as the delegate but still may not show up in your query because the persistence to backing store (database) may not have happened yet.
Bottom line, you need to wait for the first task. Make sure it's synchronous (or you may wait a long time) and then put a wait into your delegate code.