如何使用 REST API 仅获取 Camunda 人工任务的表单变量?
How to get only form variables of Camunda's human task using REST API?
我有一个简单的 bpmn,只有设置流程变量的脚本任务和接受两个输入参数、一个输出参数并定义了一个表单数据字段的人工任务。
在我启动流程后,人工任务的执行停止了。然后我试图获取人工任务表单的变量(即输入变量 and\or 表单数据字段而不是屏幕任务设置的执行变量)。
REST查询结果API如下:
- task/id/variables - 输入变量和脚本任务变量
- task/id/form-variables - 输入变量、表单数据字段和脚本任务变量
- task/id/localVariables - 空
我不明白为什么脚本任务变量与其他表单变量一起返回。
如何只获取输入变量和表单数据字段而不获取屏幕任务变量(可能还有所有其他流程变量)?
编辑:我不想对 select 变量名使用查询参数。
我正在使用 Camunda Widlfly 10.0.0。
我正在使用的 bpmn:
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="1.3.0">
<bpmn:process id="userTaskExample" isExecutable="true">
<bpmn:startEvent id="StartEvent_02mu30n">
<bpmn:outgoing>SequenceFlow_14v3dyf</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:endEvent id="EndEvent_144sl4n">
<bpmn:incoming>SequenceFlow_0acu0wh</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="SequenceFlow_0acu0wh" sourceRef="Task_096f7n7" targetRef="EndEvent_144sl4n" />
<bpmn:userTask id="Task_096f7n7" name="user task sk">
<bpmn:extensionElements>
<camunda:formData>
<camunda:formField id="form_1" label="form_1" type="string" defaultValue="form_1_" />
</camunda:formData>
<camunda:inputOutput>
<camunda:inputParameter name="input_1">input_1_val</camunda:inputParameter>
<camunda:inputParameter name="input_2">input_2_val</camunda:inputParameter>
<camunda:outputParameter name="output_1">output_2</camunda:outputParameter>
</camunda:inputOutput>
</bpmn:extensionElements>
<bpmn:incoming>SequenceFlow_0sulfwg</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_0acu0wh</bpmn:outgoing>
</bpmn:userTask>
<bpmn:sequenceFlow id="SequenceFlow_14v3dyf" sourceRef="StartEvent_02mu30n" targetRef="Task_0cphzp1" />
<bpmn:sequenceFlow id="SequenceFlow_0sulfwg" sourceRef="Task_0cphzp1" targetRef="Task_096f7n7" />
<bpmn:scriptTask id="Task_0cphzp1" name="script task sk" scriptFormat="groovy" camunda:resultVariable="script_task_variable">
<bpmn:incoming>SequenceFlow_14v3dyf</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_0sulfwg</bpmn:outgoing>
<bpmn:script><![CDATA["scipt task variable"]]></bpmn:script>
</bpmn:scriptTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="userTaskExample">
<bpmndi:BPMNShape id="StartEvent_02mu30n_di" bpmnElement="StartEvent_02mu30n">
<dc:Bounds x="151" y="66" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="169" y="102" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="EndEvent_144sl4n_di" bpmnElement="EndEvent_144sl4n">
<dc:Bounds x="616" y="66" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="634" y="102" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_0acu0wh_di" bpmnElement="SequenceFlow_0acu0wh">
<di:waypoint xsi:type="dc:Point" x="433" y="84" />
<di:waypoint xsi:type="dc:Point" x="524" y="84" />
<di:waypoint xsi:type="dc:Point" x="524" y="84" />
<di:waypoint xsi:type="dc:Point" x="616" y="84" />
<bpmndi:BPMNLabel>
<dc:Bounds x="539" y="84" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="UserTask_1lppvq8_di" bpmnElement="Task_096f7n7">
<dc:Bounds x="333" y="44" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_14v3dyf_di" bpmnElement="SequenceFlow_14v3dyf">
<di:waypoint xsi:type="dc:Point" x="169" y="102" />
<di:waypoint xsi:type="dc:Point" x="169" y="133" />
<di:waypoint xsi:type="dc:Point" x="252" y="133" />
<di:waypoint xsi:type="dc:Point" x="192" y="204" />
<di:waypoint xsi:type="dc:Point" x="275" y="204" />
<di:waypoint xsi:type="dc:Point" x="275" y="284" />
<bpmndi:BPMNLabel>
<dc:Bounds x="222" y="153.5" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="SequenceFlow_0sulfwg_di" bpmnElement="SequenceFlow_0sulfwg">
<di:waypoint xsi:type="dc:Point" x="325" y="331" />
<di:waypoint xsi:type="dc:Point" x="401" y="342" />
<di:waypoint xsi:type="dc:Point" x="383" y="204" />
<di:waypoint xsi:type="dc:Point" x="383" y="124" />
<bpmndi:BPMNLabel>
<dc:Bounds x="392" y="258" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="ScriptTask_1q3eqsu_di" bpmnElement="Task_0cphzp1">
<dc:Bounds x="225" y="284" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
您的结果表明您正在人工任务表单中显示脚本任务中定义的变量。
我这样说是因为当您查看源代码以检索我们看到的表单变量时:
// first, evaluate form fields
TaskDefinition taskDefinition = task.getTaskDefinition();
if (taskDefinition != null) {
TaskFormData taskFormData = taskDefinition.getTaskFormHandler().createTaskForm(task);
for (FormField formField : taskFormData.getFormFields()) {
if(formVariableNames == null || formVariableNames.contains(formField.getId())) {
result.put(formField.getId(), createVariable(formField, task));
}
}
}
// collect remaining variables from task scope and parent scopes
task.collectVariables(result, formVariableNames, false, deserializeObjectValues);
请注意,我们遍历表单字段定义以检索我们关心的变量名称,然后我们从执行上下文中填充数据。
最终 Activiti/Camunda(它们在引擎级别有 90% 相同)不区分在表单中输入的数据和在脚本或服务任务中创建的数据(例如作为结果变量)。
如果您出于某种原因需要将它们分开,我建议您使用命名约定并根据变量名称约定查询流程变量。
我有一个简单的 bpmn,只有设置流程变量的脚本任务和接受两个输入参数、一个输出参数并定义了一个表单数据字段的人工任务。
在我启动流程后,人工任务的执行停止了。然后我试图获取人工任务表单的变量(即输入变量 and\or 表单数据字段而不是屏幕任务设置的执行变量)。
REST查询结果API如下:
- task/id/variables - 输入变量和脚本任务变量
- task/id/form-variables - 输入变量、表单数据字段和脚本任务变量
- task/id/localVariables - 空
我不明白为什么脚本任务变量与其他表单变量一起返回。
如何只获取输入变量和表单数据字段而不获取屏幕任务变量(可能还有所有其他流程变量)? 编辑:我不想对 select 变量名使用查询参数。
我正在使用 Camunda Widlfly 10.0.0。
我正在使用的 bpmn:
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="1.3.0">
<bpmn:process id="userTaskExample" isExecutable="true">
<bpmn:startEvent id="StartEvent_02mu30n">
<bpmn:outgoing>SequenceFlow_14v3dyf</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:endEvent id="EndEvent_144sl4n">
<bpmn:incoming>SequenceFlow_0acu0wh</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="SequenceFlow_0acu0wh" sourceRef="Task_096f7n7" targetRef="EndEvent_144sl4n" />
<bpmn:userTask id="Task_096f7n7" name="user task sk">
<bpmn:extensionElements>
<camunda:formData>
<camunda:formField id="form_1" label="form_1" type="string" defaultValue="form_1_" />
</camunda:formData>
<camunda:inputOutput>
<camunda:inputParameter name="input_1">input_1_val</camunda:inputParameter>
<camunda:inputParameter name="input_2">input_2_val</camunda:inputParameter>
<camunda:outputParameter name="output_1">output_2</camunda:outputParameter>
</camunda:inputOutput>
</bpmn:extensionElements>
<bpmn:incoming>SequenceFlow_0sulfwg</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_0acu0wh</bpmn:outgoing>
</bpmn:userTask>
<bpmn:sequenceFlow id="SequenceFlow_14v3dyf" sourceRef="StartEvent_02mu30n" targetRef="Task_0cphzp1" />
<bpmn:sequenceFlow id="SequenceFlow_0sulfwg" sourceRef="Task_0cphzp1" targetRef="Task_096f7n7" />
<bpmn:scriptTask id="Task_0cphzp1" name="script task sk" scriptFormat="groovy" camunda:resultVariable="script_task_variable">
<bpmn:incoming>SequenceFlow_14v3dyf</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_0sulfwg</bpmn:outgoing>
<bpmn:script><![CDATA["scipt task variable"]]></bpmn:script>
</bpmn:scriptTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="userTaskExample">
<bpmndi:BPMNShape id="StartEvent_02mu30n_di" bpmnElement="StartEvent_02mu30n">
<dc:Bounds x="151" y="66" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="169" y="102" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="EndEvent_144sl4n_di" bpmnElement="EndEvent_144sl4n">
<dc:Bounds x="616" y="66" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="634" y="102" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_0acu0wh_di" bpmnElement="SequenceFlow_0acu0wh">
<di:waypoint xsi:type="dc:Point" x="433" y="84" />
<di:waypoint xsi:type="dc:Point" x="524" y="84" />
<di:waypoint xsi:type="dc:Point" x="524" y="84" />
<di:waypoint xsi:type="dc:Point" x="616" y="84" />
<bpmndi:BPMNLabel>
<dc:Bounds x="539" y="84" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="UserTask_1lppvq8_di" bpmnElement="Task_096f7n7">
<dc:Bounds x="333" y="44" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_14v3dyf_di" bpmnElement="SequenceFlow_14v3dyf">
<di:waypoint xsi:type="dc:Point" x="169" y="102" />
<di:waypoint xsi:type="dc:Point" x="169" y="133" />
<di:waypoint xsi:type="dc:Point" x="252" y="133" />
<di:waypoint xsi:type="dc:Point" x="192" y="204" />
<di:waypoint xsi:type="dc:Point" x="275" y="204" />
<di:waypoint xsi:type="dc:Point" x="275" y="284" />
<bpmndi:BPMNLabel>
<dc:Bounds x="222" y="153.5" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="SequenceFlow_0sulfwg_di" bpmnElement="SequenceFlow_0sulfwg">
<di:waypoint xsi:type="dc:Point" x="325" y="331" />
<di:waypoint xsi:type="dc:Point" x="401" y="342" />
<di:waypoint xsi:type="dc:Point" x="383" y="204" />
<di:waypoint xsi:type="dc:Point" x="383" y="124" />
<bpmndi:BPMNLabel>
<dc:Bounds x="392" y="258" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="ScriptTask_1q3eqsu_di" bpmnElement="Task_0cphzp1">
<dc:Bounds x="225" y="284" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
您的结果表明您正在人工任务表单中显示脚本任务中定义的变量。 我这样说是因为当您查看源代码以检索我们看到的表单变量时:
// first, evaluate form fields
TaskDefinition taskDefinition = task.getTaskDefinition();
if (taskDefinition != null) {
TaskFormData taskFormData = taskDefinition.getTaskFormHandler().createTaskForm(task);
for (FormField formField : taskFormData.getFormFields()) {
if(formVariableNames == null || formVariableNames.contains(formField.getId())) {
result.put(formField.getId(), createVariable(formField, task));
}
}
}
// collect remaining variables from task scope and parent scopes
task.collectVariables(result, formVariableNames, false, deserializeObjectValues);
请注意,我们遍历表单字段定义以检索我们关心的变量名称,然后我们从执行上下文中填充数据。
最终 Activiti/Camunda(它们在引擎级别有 90% 相同)不区分在表单中输入的数据和在脚本或服务任务中创建的数据(例如作为结果变量)。
如果您出于某种原因需要将它们分开,我建议您使用命名约定并根据变量名称约定查询流程变量。