cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass variable from one task to another task

manav
Confirmed Champ
Confirmed Champ
I have created one workflow. On human task there are some fields which would be filled up by user and those fields must be visible and editable to the other user task. In other user task when I define variable (writable & readable) in the form property, it does not show the old values entered by the first user. But when I don't define new variable , it shows the old one which is also editable but shows an error on submit.
1 ACCEPTED ANSWER

thuynh
Star Contributor
Star Contributor

Hi Manav Aahuja,

Have a look at Activiti documentation for implementing Forms here: Activiti User Guide 

In a nutshell, in order to implement user task variables correctly, you need to do the following:

- Imagine a process such as book order which consists of several service tasks and 2 user tasks as follows

- Design your process variables such as: isbn, validationResult 

- When the process instance reaches the first user task, say, review order. You would need to 'map' the process variables to form properties like below (note that I have the form property ids to be exact to process variables name to utilize the Activiti form properties auto mapping. You don't have to. Read documentation here Activiti User Guide  to know how to map form properties to process variables with different names.

<userTask id="task">   <extensionElements>     <activiti:formProperty id="isbn" />     <activiti:formProperty id="validationResult"/>   </extensionElements> </userTask>

- Then you do the same for the 2nd user task 'Final Approval'. This will map process variables to form properties displayed on form 'Final Approval'.

- I have 2 service tasks for demo purposes. First one to initialize the process variables and the second is to update validationResult variable to stimulate a successful validation after Review task.

My unit test ran successfully. I could see correct values of form properties on each user task and both user tasks were submitted successfully which end the process instance.

INFO: Task form: reviewForm
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: REVIEW FORM -- property id = caseId value = 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: REVIEW FORM -- property id = caseName value = Book order case 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: REVIEW FORM -- property id = isbn value = 343
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: REVIEW FORM -- property id = validationResult value = false
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Submit Review
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Update validation result to true for execution id 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Process execution: id: 5 and current activity id= usertask3
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Execution variables: {Case ID=null, caseId=5, ISBN=null, isbn=343, Case Name=null, validationResult=true, bookOrder=org.activiti.designer.test.BookOrder@70bd9535, caseName=Book order case 5}
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Task 2 form: finalApprovalForm
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: APPROVAL FORM -- property id = caseId value = 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: APPROVAL FORM -- property id = caseName value = Book order case 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: APPROVAL FORM -- property id = isbn value = 343
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: APPROVAL FORM -- property id = validationResult value = true
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Submit Final Approval
Dec 22, 2016 3:31:22 PM java.util.logging.LogManager$RootLogger log
INFO: Running processes: 0
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.993 sec
Dec 22, 2016 3:31:22 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.GenericApplicationContext@5d277b59: startup date [Thu Dec 22 15:31:17 ICT 2016]; root of context hierarchy

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ activiti-examples ---
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ activiti-examples ---
[INFO] Installing /home/osboxes/workspace/activiti-hello/target/activiti-examples-1.0-SNAPSHOT.jar to /home/osboxes/.m2/repository/org/activiti/examples/activiti-examples/1.0-SNAPSHOT/activiti-examples-1.0-SNAPSHOT.jar
[INFO] Installing /home/osboxes/workspace/activiti-hello/pom.xml to /home/osboxes/.m2/repository/org/activiti/examples/activiti-examples/1.0-SNAPSHOT/activiti-examples-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.149s
[INFO] Finished at: Thu Dec 22 15:31:22 ICT 2016
[INFO] Final Memory: 11M/166M
[INFO] ------------------------------------------------------------------------

So I would double check several things in your implementation:

- First, verify your process variables <-> form properties mapping and see if its done correctly

- Second, verify your submission code to see if there's any error. If you still see the error, what is it? 

I have attached my unit test code for reference,

Hope this helps,

Thanks,

Thong Huynh

View answer in original post

2 REPLIES 2

hari
Star Contributor
Star Contributor

Define variables and use them at process instance level. 

thuynh
Star Contributor
Star Contributor

Hi Manav Aahuja,

Have a look at Activiti documentation for implementing Forms here: Activiti User Guide 

In a nutshell, in order to implement user task variables correctly, you need to do the following:

- Imagine a process such as book order which consists of several service tasks and 2 user tasks as follows

- Design your process variables such as: isbn, validationResult 

- When the process instance reaches the first user task, say, review order. You would need to 'map' the process variables to form properties like below (note that I have the form property ids to be exact to process variables name to utilize the Activiti form properties auto mapping. You don't have to. Read documentation here Activiti User Guide  to know how to map form properties to process variables with different names.

<userTask id="task">   <extensionElements>     <activiti:formProperty id="isbn" />     <activiti:formProperty id="validationResult"/>   </extensionElements> </userTask>

- Then you do the same for the 2nd user task 'Final Approval'. This will map process variables to form properties displayed on form 'Final Approval'.

- I have 2 service tasks for demo purposes. First one to initialize the process variables and the second is to update validationResult variable to stimulate a successful validation after Review task.

My unit test ran successfully. I could see correct values of form properties on each user task and both user tasks were submitted successfully which end the process instance.

INFO: Task form: reviewForm
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: REVIEW FORM -- property id = caseId value = 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: REVIEW FORM -- property id = caseName value = Book order case 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: REVIEW FORM -- property id = isbn value = 343
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: REVIEW FORM -- property id = validationResult value = false
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Submit Review
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Update validation result to true for execution id 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Process execution: id: 5 and current activity id= usertask3
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Execution variables: {Case ID=null, caseId=5, ISBN=null, isbn=343, Case Name=null, validationResult=true, bookOrder=org.activiti.designer.test.BookOrder@70bd9535, caseName=Book order case 5}
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Task 2 form: finalApprovalForm
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: APPROVAL FORM -- property id = caseId value = 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: APPROVAL FORM -- property id = caseName value = Book order case 5
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: APPROVAL FORM -- property id = isbn value = 343
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: APPROVAL FORM -- property id = validationResult value = true
Dec 22, 2016 3:31:21 PM java.util.logging.LogManager$RootLogger log
INFO: Submit Final Approval
Dec 22, 2016 3:31:22 PM java.util.logging.LogManager$RootLogger log
INFO: Running processes: 0
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.993 sec
Dec 22, 2016 3:31:22 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.GenericApplicationContext@5d277b59: startup date [Thu Dec 22 15:31:17 ICT 2016]; root of context hierarchy

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ activiti-examples ---
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ activiti-examples ---
[INFO] Installing /home/osboxes/workspace/activiti-hello/target/activiti-examples-1.0-SNAPSHOT.jar to /home/osboxes/.m2/repository/org/activiti/examples/activiti-examples/1.0-SNAPSHOT/activiti-examples-1.0-SNAPSHOT.jar
[INFO] Installing /home/osboxes/workspace/activiti-hello/pom.xml to /home/osboxes/.m2/repository/org/activiti/examples/activiti-examples/1.0-SNAPSHOT/activiti-examples-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.149s
[INFO] Finished at: Thu Dec 22 15:31:22 ICT 2016
[INFO] Final Memory: 11M/166M
[INFO] ------------------------------------------------------------------------

So I would double check several things in your implementation:

- First, verify your process variables <-> form properties mapping and see if its done correctly

- Second, verify your submission code to see if there's any error. If you still see the error, what is it? 

I have attached my unit test code for reference,

Hope this helps,

Thanks,

Thong Huynh