cancel
Showing results for 
Search instead for 
Did you mean: 

Field Injection for UserTasks

prasanthnath_g
Champ in-the-making
Champ in-the-making
From the documentation, I see that field injection is supported for Service tasks. I just wanted to know if the same is supported for user tasks too. Something like this:

<userTask id="auditPhase1" name="Audit Phase 1">
   <extensionElements>
      <activiti:field name="actions" stringValue="Approve,Reject,Reprocess" />
   </extensionElements>
</userTask>


We have a requirement to show the list of tasks defined in the work flow and supported actions for each task. There are several audit tasks which are modeled as user tasks in our workflow. And each audit task can have different actions supported. For ex: Approval, rejection, Re-process data. So I just wanted to know if I can store that information in user task definition itself.

Here is how we are fetching task details:

ProcessInstance processInstance = getProcessInstance(dataId);
BpmnModel model = getBpmnModelForProcessDefinition(processInstance.getProcessDefinitionId());
List<TaskDetail> taskDetails = Lists.newArrayList();
for(Object obj : model.getMainProcess().getFlowElements()) {
   if(obj instanceof ServiceTask) {
      ServiceTask serviceTask = (ServiceTask)obj;
      taskDetails.add(new TaskDetail(serviceTask));
   } else if(obj instanceof UserTask) {
      UserTask userTask = (UserTask)obj;
      taskDetails.add(new TaskDetail(userTask));
   }
}

So now if the actions can be stored as fields in the user task definition itself, then I can fetch and store that information in the above code. Please suggest!
12 REPLIES 12

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi Prasanth,

Yes it is supported.

Regards
Martin

prasanthnath_g
Champ in-the-making
Champ in-the-making
I tried doing the same but could not get access to the fields. On debugging the code below, I am always getting extension elements(from which i can get fields) as empty map for the user task:
<java>
ProcessInstance processInstance = getProcessInstance(dataId);
BpmnModel model = getBpmnModelForProcessDefinition(processInstance.getProcessDefinitionId());
List<TaskDetail> taskDetails = Lists.newArrayList();
for(Object obj : model.getMainProcess().getFlowElements()) {
if(obj instanceof ServiceTask) {
  ServiceTask serviceTask = (ServiceTask)obj;
  taskDetails.add(new TaskDetail(serviceTask));
} else if(obj instanceof UserTask) {
  UserTask userTask = (UserTask)obj;
                Map<String, List<ExtensionElement>> extensionElements = userTask.getExtensionElements();
         System.out.println(extensionElements);
  taskDetails.add(new TaskDetail(userTask));
}
}
</java>

Is this the way to access the fields? Or do i need to access them in some other way?

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi Prasanth,

Did you check your process definition?
Try to add extension element to userTask

    <userTask id="usertask" name="Task" activiti:candidateGroups="dummy">
      <extensionElements>
        <activiti:newExtensionElement/>


and put break point :

        } else if (inExtensionElements) {
          ExtensionElement extensionElement = BpmnXMLUtil.parseExtensionElement(xtr);
          parentElement.addExtensionElement(extensionElement);
        }

Regards
Martin

prasanthnath_g
Champ in-the-making
Champ in-the-making
Sorry. I did not get you. Here is how I have currently defined my user task:
<code>
<userTask id="audit1" name="Audit Phase 1" activiti:resultVariable="status">
<extensionElements>
  <activiti:field name="actions" stringValue="Approve,Reject,Reprocess" />
</extensionElements>
</userTask>
</code>
I am using Activiti 5.15 as of now. What is activiti:newExtensionElement? Is this a new element supported by Activiti? Can I have more information on this?

prasanthnath_g
Champ in-the-making
Champ in-the-making
Thanks for the pointer, Martin. After debugging the code, I found this line in FieldExtensionParser:
<java>
public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model) throws Exception {
   
    if (parentElement instanceof ActivitiListener == false && parentElement instanceof ServiceTask == false &&
        parentElement instanceof SendTask == false) return;

    /** Code **/
}
</java>

So it seems for user tasks, I can not add fields. Can some one please suggest if there is any other way of achieving the same with user tasks?

trademak
Star Contributor
Star Contributor
Hi Prasanth,

Field extensions are not supported for user tasks as is shown in your code example.
What you can do is add a field element in your own namespace. This field element will then be available in the BpmnModel object of your process definition which can be retrieved via the RepositoryService. When you lookup the UserTask object it will have extension elements which contains the field element values.

Best regards,

For getting this done, do I need to write some custom parser logic? It would be really helpful if you can share some example.

trademak
Star Contributor
Star Contributor
No, you don't need any custom parser logic. The extension elements will be available in the user task by default if you add a custom extension element like:

<custom:field xmlns:custom="http://activiti.org/custom" name="actions" stringValue="Approve,Reject,Reprocess" />

It seems the example code you have posted is missing. Can you please repost it?