如何访问多实例子流程内Call Activity的输出参数?
解决方案:在串行多实例子流程外部访问Call Activity输出参数
针对你在Camunda流程中遇到的问题——如何在串行多实例子流程外部访问Loop Over Choices Call Activity的输出参数var_post_choice_ad,并在后续Post Loop Over Step中使用,分两种场景给出具体实现方式:
场景1:仅需要最后一次循环的输出值
你的子流程是串行多实例,默认每次循环的输出变量会覆盖上一次的值。只要确保子流程能将变量传递到父流程作用域,就能直接在后续Call Activity中使用。
具体配置步骤:
配置子流程暴露变量到父流程
在子流程的扩展元素中添加<camunda:out variables="all" />,让子流程内的变量同步到父流程。修改后的子流程XML片段:<bpmn:subProcess id="Activity_1" name="Loop over choices"> <bpmn:outgoing>Flow_1dqpuh2</bpmn:outgoing> <bpmn:multiInstanceLoopCharacteristics isSequential="true" camunda:collection="selected_choices_list" camunda:elementVariable="current_choice" /> <bpmn:extensionElements> <camunda:out variables="all" /> </bpmn:extensionElements> <!-- 子流程内部原有元素保持不变 --> </bpmn:subProcess>在后续Call Activity中直接引用变量
在Post Loop Over Step的step_parameters脚本中直接使用var_post_choice_ad,示例修改:<camunda:inputParameter name="step_parameters"> <camunda:script scriptFormat="javascript">var params = { "id": 2, "name": "Post Step Id", "last_choice_result": var_post_choice_ad } JSON.stringify(params)</camunda:script> </camunda:inputParameter>
场景2:需要收集所有循环的输出值
如果需要保留每次循环的var_post_choice_ad结果,需在父流程中初始化列表变量,每次循环时将结果追加到列表中。
具体配置步骤:
初始化列表变量
在流程启动事件中添加脚本,创建空列表存储所有结果:<bpmn:startEvent id="StartEvent_1"> <bpmn:extensionElements> <camunda:executionListener event="start"> <camunda:script scriptFormat="javascript">execution.setVariable("all_choice_results", []);</camunda:script> </camunda:executionListener> </bpmn:extensionElements> </bpmn:startEvent>修改Call Activity的输出脚本,追加结果到列表
更新var_post_choice_ad的输出脚本,同时将结果添加到父流程的列表中:<camunda:outputParameter name="var_post_choice_ad"> <camunda:script scriptFormat="javascript"> var result; if (typeof val_after_choice !== 'undefined') { result = (val_after_choice > 0 || var_post_choice_ad).toString(); } else { result = var_post_choice_ad; } // 将结果追加到父流程的列表变量 var allResults = execution.getParent().getVariable("all_choice_results"); allResults.push(result); execution.getParent().setVariable("all_choice_results", allResults); return result; </camunda:script> </camunda:outputParameter>在后续Call Activity中使用列表变量
直接引用all_choice_results获取所有循环结果,示例修改:<camunda:inputParameter name="step_parameters"> <camunda:script scriptFormat="javascript">var params = { "id": 2, "name": "Post Step Id", "all_choice_results": all_choice_results } JSON.stringify(params)</camunda:script> </camunda:inputParameter>
关键说明
- 串行多实例的每个子流程实例拥有独立变量作用域,通过
execution.getParent()可访问父流程变量。 - 使用
<camunda:out variables="all" />会将子流程变量同步到父流程,但会覆盖同名变量,因此收集所有结果时需用列表存储。
内容的提问来源于stack exchange,提问作者javaseeker
相关产品推荐
相关产品推荐

