cancel
Showing results for 
Search instead for 
Did you mean: 

List all user tasks from bpm file

darshan_hardas
Champ in-the-making
Champ in-the-making
We have a requirement to list all the user task/tasks defined for a process from an BPM file. I have used the TaskService query filtered with process instance id but all task are not listed.

<code>
String processInstanceId = processInstance.getId();
List<Task> instanceTaskList = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
<code>

On some of the forums, it is mentioned to use HistoricActivityInstance methods to populate the task. We have also used the below code.

For unfinished tasks:
<code>
List<HistoricActivityInstance>  historicActivityInstances =
                                  historyService.createHistoricActivityInstanceQuery()
              .processDefinitionId(processInstance.getProcessDefinitionId())
              .unfinished()
              .listPage(0, 10);
<code>

For finished tasks:
<code>
List<HistoricActivityInstance>  historicActivityInstances =
                                  historyService.createHistoricActivityInstanceQuery()
              .processDefinitionId(processInstance.getProcessDefinitionId())
              .finished()
              .listPage(0, 10);
<code>

But it does not give us all the tasks defined.

For a test purpose we have used "Vacation Request" as an example.
BPM file :

<code>
<?xml version="1.0" encoding="UTF-8" ?>
<definitions id="definitions"
  targetNamespace="http://activiti.org/bpmn20"
  xmlns:activiti="http://activiti.org/bpmn"
  xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
  xmlnsSmiley Surprisedmgdc="http://www.omg.org/spec/DD/20100524/DC"
  xmlnsSmiley Surprisedmgdi="http://www.omg.org/spec/DD/20100524/DI"
  xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <process id="vacationRequest" name="Vacation request">
 
    <startEvent id="request" activiti:initiator="employeeName">
      <extensionElements>
        <activiti:formProperty id="numberOfDays" name="Number of days" type="long" required="true">1</activiti:formProperty>
        <activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" type="date" required="true" />
        <activiti:formProperty id="vacationMotivation" name="Motivation" type="string" />
      </extensionElements>
    </startEvent>
    <sequenceFlow id="flow1" sourceRef="request" targetRef="handleRequest" />
   
    <userTask id="handleRequest" name="Handle vacation request" >
      <documentation>
        ${employeeName} would like to take ${numberOfDays} day(s) of vacation (Motivation: ${vacationMotivation}).
      </documentation>
      <extensionElements>
         <activiti:formProperty id="vacationApproved" name="Do you approve this vacation" type="enum" required="true">
          <activiti:value id="true" name="Approve" />
          <activiti:value id="false" name="Reject" />
        </activiti:formProperty>
        <activiti:formProperty id="managerMotivation" name="Motivation" type="string" />
      </extensionElements>
      <potentialOwner>
        <resourceAssignmentExpression>
          <formalExpression>Triage Queue</formalExpression>
        </resourceAssignmentExpression>
      </potentialOwner>        
    </userTask>
    <sequenceFlow id="flow2" sourceRef="handleRequest" targetRef="requestApprovedDecision" />
   
    <exclusiveGateway id="requestApprovedDecision" name="Request approved?" />
    <sequenceFlow id="flow3" sourceRef="requestApprovedDecision" targetRef="sendApprovalMail">
      <conditionExpression xsi:type="tFormalExpression">${vacationApproved == 'true'}</conditionExpression>
    </sequenceFlow>
   
    <task id="sendApprovalMail" name="Send confirmation e-mail" />
    <sequenceFlow id="flow4" sourceRef="sendApprovalMail" targetRef="theEnd1" />
    <endEvent id="theEnd1" />
   
    <sequenceFlow id="flow5" sourceRef="requestApprovedDecision" targetRef="adjustVacationRequestTask">
      <conditionExpression xsi:type="tFormalExpression">${vacationApproved == 'false'}</conditionExpression>
    </sequenceFlow>
   
    <userTask id="adjustVacationRequestTask" name="Adjust vacation request">
      <documentation>
        Your manager has disapproved your vacation request for ${numberOfDays} days.
        Reason: ${managerMotivation}
      </documentation>
      <extensionElements>
        <activiti:formProperty id="numberOfDays" name="Number of days" type="long" required="true">${numberOfDays}</activiti:formProperty>
        <activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" type="date" required="true" />
        <activiti:formProperty id="vacationMotivation" name="Motivation" type="string" >${vacationMotivation}</activiti:formProperty>
        <activiti:formProperty id="resendRequest" name="Resend vacation request to manager?" type="enum" required="true">
          <activiti:value id="true" name="Yes" />
          <activiti:value id="false" name="No" />
        </activiti:formProperty>
      </extensionElements>
      <humanPerformer>
        <resourceAssignmentExpression>
          <formalExpression>${employeeName}</formalExpression>
        </resourceAssignmentExpression>
      </humanPerformer> 
    </userTask>
    <sequenceFlow id="flow6" sourceRef="adjustVacationRequestTask" targetRef="resendRequestDecision" />
   
    <exclusiveGateway id="resendRequestDecision" name="Resend request?" />
    <sequenceFlow id="flow7" sourceRef="resendRequestDecision" targetRef="handleRequest">
      <conditionExpression xsi:type="tFormalExpression">${resendRequest == 'true'}</conditionExpression>
    </sequenceFlow>
   
     <sequenceFlow id="flow8" sourceRef="resendRequestDecision" targetRef="theEnd2">
      <conditionExpression xsi:type="tFormalExpression">${resendRequest == 'false'}</conditionExpression>
    </sequenceFlow>
    <endEvent id="theEnd2" />
     
  </process>
 
</definitions>

<code>

Please guide us with your comments. Thanks in advance.
2 REPLIES 2

frederikherema1
Star Contributor
Star Contributor
The taskService only returns actual active tasks, not possible future tasks in a process. Use repositoryService.getBpmnModel(…) which can be inspected to find all Activities that are UserTasks.

Thanks for the response .

I have got it solved by the way you suggested.

<code>
ProcessDefinitionEntity processDefinition =
    (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService).getDeployedProcessDefinition(processInstance.getProcessDefinitionId());
       
        if (processDefinition != null) {
            for (ActivityImpl activity : processDefinition.getActivities()) {
              String type = (String) activity.getProperty("type");
              String name = (String) activity.getProperty("name");
              String taskId = activity.getId();
            
              System.out.println("test "+type+" name "+name + " id: " + taskId);
             
          
            }
        }
</code>