There are several ways to get the active task/s from a process instance. In this case, we’re using the TaskListService that returns an Observable. You will need to add it to the constructor.
import { TaskQueryRequestRepresentationModel, TaskListService, TaskDetailsModel } from '@alfresco/adf-process-services';
.
constructor (private taskListService: TaskListService) {}
To prepare the query we will use a TaskQueryRequestRepresentationModel object that will contain the state of the task (‘active’) and the process instance ID.
/** * Gets an active task query to filter by process instance Id * @param {string} processInstanceId process instance ID * @returns {TaskQueryRequestRepresentationModel} */ getActiveTaskQuery(processInstanceId: string) { return this.getTaskQuery('active', processInstanceId); }
/** * Gets a task query to filter by task state and process instance ID * @param {string} state task state * @param {string} processInstanceId process instance ID * @returns {TaskQueryRequestRepresentationModel} */ getTaskQuery(state: string, processInstanceId: string) { const taskQuery = new TaskQueryRequestRepresentationModel(); taskQuery.state = state; taskQuery.processInstanceId = processInstanceId; return taskQuery; }
If your process model doesn’t include a parallel gateway you can assume there will be only one active task per process instance.
/** * Gets the active task if there's only one active task, throws an error if there are more than one * @param {string} processInstanceId process instance ID to filter by * @returns {Observable} */ getActiveTask(processInstanceId: string): Observable<TaskDetailsModel> { return this.taskListService.getTasks(this.getActiveTaskQuery(processInstanceId)).map((taskList: TaskListModel) => { if (taskList.data.length === 1) { return taskList.data[0]; } else if (taskList.data.length > 1) { throw new Error(`There are multiple active tasks, process instance ID: ${processInstanceId}`); } }, error => { this.logger.error('Error: ', error); }); }
If your process model includes a parallel gateway you might get multiple active tasks from the same process instance.
/** * Gets the list of active tasks * @param {string} processInstanceId process instance ID to filter by * @returns {Observable} */ getActiveTaskList(processInstanceId: string): Observable<TaskListModel> { return this.taskListService.getTasks(this.getActiveTaskQuery(processInstanceId)); }