Workflow

How to Complete a Workflow Task Programmatically

Updated: July 17, 2023

You might need this to implement some sort of automatic processing instead of having the user completing the task from the UI.

Basically, you have to call:

    void endTask(CoreSession session, Task task, Map<String, Object> data,  String status) throws ClientException;

on the DocumentRoutingService, where:

  • task is the Task to end;
  • data is a map of variables. If a variable called "comment" is contained in this map, its value will be logged by the audit service under the "workflowTaskCompleted" event. When a user submits the task form (configured via Studio), this map contains all the variables in the form; If you want to set an existing Workflow or a Node variable when completing the task you can add them into this map.
  • status is the id of the button the user would have clicked to submit the task form (if the outgoing transitions of the workflow node that created the task have conditions depending on it). This id is the button id you specified when you configured the task form in Studio.

In your graph, you have nodes and transitions between these nodes. When the workflow enters a node, if that node is of type Task (that means is not an automatic one, like Start and Stop for example), a task is created at that step. This task is persisted as a document model, that can be adapted to the Task object. When a task is ended, the workflow is resumed. The task is holding information about the node and the workflow instance it was created from, so when the task is ended using the API above, the related workflow instance is resumed.

To fetch all the open tasks assigned to a given user, on a document use:

  List<Task> getTaskInstances(DocumentModel dm, NuxeoPrincipal user, CoreSession coreSssion) throws ClientException;

on the TaskService.

To fetch all the open tasks originating from the same node in the workflow use:

List<Task> getAllTaskInstances(String processId, String nodeId, CoreSession session) throws ClientException;

where:

  • processId is the id of the document representing the workflow instance.
  • node id, is a unique identifier of that node in the workflow, generated by Studio. It's listed on the General tab, on the node popup-up when editing a workflow node in Studio.

For some detailed examples on how to use this API, you can check the JUnit tests in the GraphRouteTest.