cancel
Showing results for 
Search instead for 
Did you mean: 

How to persist (archive) user task form property data?

bbarani
Champ in-the-making
Champ in-the-making
Hi,

I am trying to archive the task along with custom form properties.

Please find below the user task… I am calling the archive service task (java class) once the user selects one of the enum value (Yes or No).

<userTask id="confirmApproval" name="Approve the picture request"
         activiti:candidateUsers="${user}" activiti:assignee="${user}">
         <extensionElements>

            <activiti:formProperty id="initiator" name="Initiator:"
               type="string" />

            <activiti:formProperty id="approvalConfirmed"
               name="approvalConfirmed" type="enum" required="true">
               <activiti:value id="true" name="Yes"></activiti:value>
               <activiti:value id="false" name="No"></activiti:value>
            </activiti:formProperty>
         </extensionElements>
      </userTask>

Java Class:

public class ArchiveTask implements org.activiti.engine.delegate.JavaDelegate

{
   public void execute(DelegateExecution execution) throws Exception {
      Map<String, String> properties = new HashMap<String, String>();

      String assigneeName = (String) execution.getVariable("user");
      boolean approval = (Boolean) execution.getVariable("needsApproval");
      TaskService taskService = execution.getEngineServices()
            .getTaskService();
      List<Task> tasks = taskService.createTaskQuery()
            .taskAssignee(assigneeName).list();
      for (Task task : tasks) {
         task.setOwner(assigneeName);

      }
      
       ProcessDefinition definition = execution.getEngineServices().getRepositoryService()
                  .createProcessDefinitionQuery().processDefinitionKey("pictureRequestNew").singleResult();
      
      FormService formService = execution.getEngineServices()
            .getFormService();
      
         List<FormProperty> formList = formService.getStartFormData(definition.getId()).getFormProperties();
            
         System.out.println(formList.size()+" Form List size");
         System.out.println(definition.getId()+" Definition Id ");
        
      try {
         properties = new HashMap<String, String>();
         properties.put("initiator", "kermit");
         properties.put("approvalConfirmed", "Yes");
         formService.submitTaskFormData(definition.getId(), properties);
      } catch (ActivitiException e) {

      }


   }
}

For some reason my formList size is always 0, can someone let me know what I am doing wrong?

Thanks,
Barani
4 REPLIES 4

trademak
Star Contributor
Star Contributor
Hi Barani,

When you are using user tasks you should use the getTaskFormData method on the FormService not the getStartFormData.

Best regards,

bbarani
Champ in-the-making
Champ in-the-making
Thanks for your reply.

I already tried the below method but I am getting the below error:

List<FormProperty> formList = formService.getTaskFormData(definition.getId()).getFormProperties(); // Throws below error

Caused by: org.activiti.engine.ActivitiException: No task found for taskId 'pictureRequestNew:1:36903'

This is what I am trying to do and I am not sure if I am doing right way..

After user enters the information in the form (user task), I am just trying to archive the form data (custom form property along with task information).

For that I am calling a Java class (service task) once the user hits complete task (by invoking a service task).

Please find below my process definition.

<process id="pictureRequestNew" name="Testing Complex Approval process">

  <startEvent id="theStart" activiti:initiator="initiator" />

  <sequenceFlow id="flow1" sourceRef="theStart" targetRef="needsApprovalTask" />

  <serviceTask id="needsApprovalTask" name="Needs approval"
   activiti:class="com.picturerequest.NeedsApprovalDelegate" />

  <sequenceFlow id="flow2" sourceRef="needsApprovalTask"
   targetRef="gateway" />

  <exclusiveGateway id="gateway" name="Needs approval?" />

  <sequenceFlow id="flow3" sourceRef="gateway" targetRef="confirmApproval">
   <conditionExpression xsi:type="tFormalExpression">
    ${needsApproval}
   </conditionExpression>
  </sequenceFlow>

  <sequenceFlow id="flow4" sourceRef="gateway" targetRef="Email">
   <conditionExpression xsi:type="tFormalExpression">
    ${!needsApproval}
   </conditionExpression>
  </sequenceFlow>

  <userTask id="confirmApproval" name="Approve the picture request"
   activiti:candidateUsers="${user}" activiti:assignee="${user}">
   <extensionElements>
    <activiti:formProperty id="initiator" name="Initiator:"
     type="string" />

    <activiti:formProperty id="approvalConfirmed"
     name="approvalConfirmed" type="enum" required="true">
     <activiti:value id="true" name="Yes"></activiti:value>
     <activiti:value id="false" name="No"></activiti:value>
    </activiti:formProperty>
   </extensionElements>
  </userTask>


  <sequenceFlow id="flow5" sourceRef="confirmApproval"
   targetRef="gateway2" />

  <exclusiveGateway id="gateway2" name="Is approval confirmed?" />

  <sequenceFlow id="flow6" sourceRef="gateway2" targetRef="chargeAccount">
   <conditionExpression xsi:type="tFormalExpression"><![CDATA[${approvalConfirmed == 'true'}]]></conditionExpression>
  </sequenceFlow>

  <sequenceFlow id="flow7" sourceRef="gateway2" targetRef="ArchiveTask">
   <conditionExpression xsi:type="tFormalExpression"><![CDATA[${approvalConfirmed == 'false'}]]></conditionExpression>
  </sequenceFlow>

  <serviceTask id="Email" name="Email Service"
   activiti:class="com.picturerequest.ChargeDelegate">
   <extensionElements>
    <activiti:field name="wsdl"
     expression="http://localhost:8521/mailService?wsdl" />
    <activiti:field name="operation" expression="chargeAccount" />
    <activiti:field name="parameters" expression="${user}, ${imageName}" />
    <activiti:field name="returnValue" expression="myReturn" />
   </extensionElements>
  </serviceTask>

  <serviceTask id="chargeAccount" name="Charge account"
   activiti:class="com.picturerequest.ChargeDelegate">
   <extensionElements>
    <activiti:field name="wsdl"
     expression="http://localhost:8291/chargeService?wsdl" />
    <activiti:field name="operation" expression="chargeAccount" />
    <activiti:field name="parameters" expression="${user}, ${imageName}" />
    <activiti:field name="returnValue" expression="myReturn" />
   </extensionElements>
  </serviceTask>

  <sequenceFlow id="flow10" sourceRef="chargeAccount"
   targetRef="ArchiveTask" />

  <sequenceFlow id="flow13" sourceRef="Email" targetRef="theEnd" />

  <serviceTask id="ArchiveTask" name="serviceTask"
   activiti:class="com.picturerequest.ArchiveTask">
  </serviceTask>

  <sequenceFlow id="flow8" sourceRef="ArchiveTask"
   targetRef="theEnd" />

  <endEvent id="theEnd" />

</process>
</definitions>


Java code:

public class ArchiveTask implements org.activiti.engine.delegate.JavaDelegate

{
public void execute(DelegateExecution execution) throws Exception {
  Map<String, String> properties = new HashMap<String, String>();

  String assigneeName = (String) execution.getVariable("user");
  boolean approval = (Boolean) execution.getVariable("needsApproval");
  TaskService taskService = execution.getEngineServices()
    .getTaskService();
  List<Task> tasks = taskService.createTaskQuery()
    .taskAssignee(assigneeName).list();

  for (Task task : tasks) {
   task.setOwner(assigneeName);

  }
 
 
   ProcessDefinition definition = execution.getEngineServices().getRepositoryService()
              .createProcessDefinitionQuery().processDefinitionKey("pictureRequestNew").singleResult();
 
  FormService formService = execution.getEngineServices()
    .getFormService();
 
       List<FormProperty> formList = formService.getTaskFormData(definition.getId()).getFormProperties(); // Throws error
    

  try {
   properties = new HashMap<String, String>();
   properties.put("initiator", "kermittttttttt");
   properties.put("approvalConfirmed", "Yes");
   formService.submitTaskFormData(definition.getId(), properties); // throws error
  } catch (ActivitiException e) {

  }
}


Overall goal….I am just trying to persist the data entered in the form by the user….

frederikherema1
Star Contributor
Star Contributor

List<FormProperty> formList = formService.getTaskFormData(definition.getId()).getFormProperties(); // Throws below error

You get the task-form using the task-id on formService.getTaskFormData OR you use process-definition-id to get start-form-properties using getStartForm(…). In your case, you just want the stuff that is in the task.

I'm not really sure what your use case is… Are you staring a new process from within another process, posting form-properties to the new process?

bbarani
Champ in-the-making
Champ in-the-making

List<FormProperty> formList = formService.getTaskFormData(definition.getId()).getFormProperties(); // Throws below error

You get the task-form using the task-id on formService.getTaskFormData OR you use process-definition-id to get start-form-properties using getStartForm(…). In your case, you just want the stuff that is in the task.

I'm not really sure what your use case is… Are you staring a new process from within another process, posting form-properties to the new process?


I am simply trying to persist my form property after the user approves/rejects the request. Please find below my use case..

External System invokes a workflow process and submits the info to Activiti –> Activiti creates a process and populates the form with the data submitted by external system —> User logs in to Activiti and approves / denies the form —> The task along with form property should get saved in history tables (archive tab).

I call a service task (archive task) after the decision of the user. The service task just sets the owner of the task and submits the form using submittaskformdata (something like below) and then the whole process comes to and end.

My BPMN xml:

  <userTask id="confirmApproval" name="Approve the picture request"
   activiti:candidateUsers="${user}" activiti:assignee="${user}">
   <extensionElements>
    <activiti:formProperty id="approvalFlag" name="Needs Approval:"
     expression="${needsApproval}" writable="false" type="boolean" />

    <activiti:formProperty id="initiatorName" name="Initiator:"
     expression="${initiator}" writable="false" type="string" />

    <activiti:formProperty id="descriptionValue" name="Description:"
     expression="${description}" readable="true" writable="false" type="string" />

    <activiti:formProperty id="userName" name="User:"
     expression="${user}" writable="false" type="string" />

    <activiti:formProperty id="imageName" name="Image:"
     expression="${imageName}" writable="false" type="string" />

    <activiti:formProperty id="approvalConfirmed"
     name="approvalConfirmed" type="enum" required="true">
     <activiti:value id="true" name="Yes"></activiti:value>
     <activiti:value id="false" name="No"></activiti:value>
    </activiti:formProperty>
   </extensionElements>
  </userTask>

<sequenceFlow id="flow5" sourceRef="confirmApproval" targetRef="gateway2" />

<exclusiveGateway id="gateway2" name="Is approval confirmed?" />

<sequenceFlow id="flow6" sourceRef="gateway2" targetRef="chargeAccount">

<conditionExpression xsi:type="tFormalExpression"><![CDATA[${approvalConfirmed == 'true'}]]></conditionExpression>
  </sequenceFlow>
  <sequenceFlow id="flow7" sourceRef="gateway2" targetRef="ArchiveTask">
   <conditionExpression xsi:type="tFormalExpression"><![CDATA[${approvalConfirmed == 'false'}]]>     </conditionExpression>
  </sequenceFlow>

<serviceTask id="chargeAccount" name="Charge" activiti:class="com.picturerequest.ChargeAccount" />

<sequenceFlow id="flowNew" sourceRef="chargeAccount" targetRef="ArchiveTask" />

<serviceTask id="ArchiveTask" name="ArchiveData" activiti:class="com.picturerequest.ArchiveTask" />

<sequenceFlow id="flow8" sourceRef="ArchiveTask" targetRef="theEnd" />



TaskFormData formData = formService.getTaskFormData(task1.getId());
task1.setOwner("fozzie");
List<FormProperty> formList = formService.getTaskFormData(formData.getTask().getId()).getFormProperties();
properties = new HashMap<String, String>();
properties.put("approvalFlag", formList.get(0).getValue());
properties.put("initiatorName",formList.get(1).getValue());
properties.put("descriptionValue", formList.get(2).getValue());
properties.put("userName", formList.get(3).getValue());
properties.put("imageName",formList.get(4).getValue());
properties.put("approvalConfirmed",formList.get(5).getValue());
formService.submitTaskFormData(formData.getTask().getId(), properties);