cancel
Showing results for 
Search instead for 
Did you mean: 

Pass variables from sub-process to process.

symphony_person
Champ in-the-making
Champ in-the-making
I am implementing a call activity with a sub process. I need to pass variables into sub process and it will pass variables back to the parent. I am able to pass variables into the sub-process but unable to pass the variables back to the parent process. I am attaching the template test case. Also found a similar thread abandoned 2 years ago:

https://forums.activiti.org/content/passing-variables-sub-process-process

Is this still a bug? Please help. Please rename the attached extension to zip as only txt is allowed.
2 REPLIES 2

symphony_person
Champ in-the-making
Champ in-the-making
Updating with some snippets:

Child:
<code>
  <process id="helloworld" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <serviceTask id="servicetask1" name="Service Task" activiti:class="org.activiti.HelloWorld"></serviceTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
    <dataObject id="input" name="input" itemSubjectRef="xsd:string"></dataObject>
    <dataObject id="output" name="output" itemSubjectRef="xsd:string"></dataObject>
  </process></code>

Parent:
<code><process id="parent" name="parent" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <callActivity id="callactivity1" name="Call activity" calledElement="helloworld">
      <extensionElements>
        <activiti:in source="name" target="input"></activiti:in>
        <activitiSmiley Surprisedut source="output" target="message"></activitiSmiley Surprisedut>
      </extensionElements>
    </callActivity>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="callactivity1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="callactivity1" targetRef="endevent1"></sequenceFlow>
    <dataObject id="name" name="name" itemSubjectRef="xsd:string"></dataObject>
    <dataObject id="message" name="message" itemSubjectRef="xsd:string">
      <extensionElements>
        <activiti:value></activiti:value>
      </extensionElements>
    </dataObject>
  </process></code>

HelloWorld
<java>public class HelloWorld extends TaskActivityBehavior {

@Override
public void execute(ActivityExecution execution) throws Exception {
  String input = (String) execution.getVariable("input");

  String message = "Hello " + input;
  System.out.println(message);
  execution.setVariable("output", message);
  System.out.println("Variables before the sub process completes:" + execution.getVariables());
}

}</java>

Test:

<java>public class MyUnitTest {

@Rule
public ActivitiRule activitiRule = new ActivitiRule();

@Test
@Deployment(resources = { "org/activiti/test/helloworld.bpmn",
   "org/activiti/test/parent.bpmn" })
public void testParent() {

  final Map<String, Object> variableMap = new HashMap<>();
  variableMap.put("name", "MIKE");

  ProcessInstance processInstance = activitiRule.getRuntimeService()
    .startProcessInstanceByKey("parent", variableMap);
  final Map<String, Object> processVariables = activitiRule
    .getRuntimeService().getVariables(processInstance.getId());

  assertEquals("Hello MIKE", processVariables.get("message"));
}

@Test
@Deployment(resources = { "org/activiti/test/helloworld.bpmn" })
public void testChild() {

  final Map<String, Object> variableMap = new HashMap<>();
  variableMap.put("input", "MIKE");

  ProcessInstance processInstance = activitiRule.getRuntimeService()
    .startProcessInstanceByKey("helloworld", variableMap);
  final Map<String, Object> processVariables = activitiRule
    .getRuntimeService().getVariables(processInstance.getId());

  assertEquals("Hello MIKE", processVariables.get("output"));
}

}</java>

symphony_person
Champ in-the-making
Champ in-the-making
I wasn't calling leave() in my HelloWorld -TaskActivityBehavior. The call activity never completed and output variables never propagated to the parent process. Thanks.