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.
1 2 3 4 5 | import { TaskQueryRequestRepresentationModel, TaskListService, TaskDetailsModel } from '@alfresco/adf-process-services' ; |
.
1 2 | 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.
1 2 3 4 5 6 7 8 | /** * 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); } |
1 2 3 4 5 6 7 8 9 10 11 12 | /** * 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * 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.
1 2 3 4 5 6 7 8 | /** * 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)); } |