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.


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


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:

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


For finished tasks:

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


But it does not give us all the tasks defined.

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


<?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"
  xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
  xmlns:omgdi="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>



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

jbarrez
Star Contributor
Star Contributor
The taskservice will only give you existing tasks. The historyservice will only give you completed tasks.

Use the following method on the RepositoryService to get a POJO representation of your process, allowing you to get all user tasks defined in the process:

<code>
  /**
   * Returns the {@link BpmnModel} corresponding with the process definition with
   * the provided process definition id. The {@link BpmnModel} is a pojo versions
   * of the BPMN 2.0 xml and can be used to introspect the process definition
   * using regular Java.
   */
  BpmnModel getBpmnModel(String processDefinitionId);
</code>

Thanks for the response .

Below is the code I have used

<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>

kpkvasan
Champ in-the-making
Champ in-the-making
Hi JoramBarrez,
                       I am using Activiti integrated with Liferay. I want to fetch all the task name associated with the Process Definition irrespective of the completed status. I tried using the Liferay-Activiti service,but no improvement. Please help me to get the task name.

Thanks in Advance.

c_arunrathnakum
Champ in-the-making
Champ in-the-making
Hi
How do i do the same using activiti-5.11? Can anybody helP?

jbarrez
Star Contributor
Star Contributor
You can't, it wasn't implemented back then.

ambrish
Champ in-the-making
Champ in-the-making
hi i am new one of activiti i'm trying to do service task in activiti,but after the deploying the process ,when start process means i getting "class couldnot be instansiated" error pls help me….
thanks in advance..

regards,
Ambrish

frederikherema1
Star Contributor
Star Contributor
Make sure the class you are referencing is on your classpath (the jar containing the .class-file or the .class-file itself) and double-check the fully-qualified name of the class is correct..

Next time, create new topics for now questions instead of piggy-backing on old ones, may be lost otherwise…

medel
Champ in-the-making
Champ in-the-making
what activiti version was implementd in ? 5.13 ?
i tried that i got a CLASSCAST  Exception : java.lang.ClassCastException: org.jboss.weld.proxies.RepositoryService$885673968$Proxy$_$$_WeldClientProxy cannot be cast to org.activiti.engine.impl.RepositoryServiceImpl

mashrur
Champ on-the-rise
Champ on-the-rise
I have the same problem as medel. It seems activiti community is too slow to response this Smiley Sad