I’m using Camunda 7.5.0. In my BPMN, I have a multi-instance subprocess. Inside, there are user forms that are required to fill in.
For example a shipping taks with form values like ‘shipment date’, ‘shipment ref’ and ‘shipment comment’.
Sometimes though, it can be decided, to ship all material in the same box. These form values must then manually be entered in every single shipping task of the multi-instance subprocess. In rare cases, the number of tasks can go up to about 20 or 30 so the packager manager asked to place a button in the forms that, if pressed, copies all these variable values to the other subprocess instances. (via rest-request with values as JSON payload in the body)
I’ve mashed together some lines of code how I would deal with this with my limited knowledge of Camunda. My question now; is that this is the best way to proceed, or are there other, more robust solutions?
public void copyShipmentVariables(String processInstanceId, Date date, String comment, String reference) {
// Get subProcess execution Ids
List<String> subProcessExecutionIds = engineServices.getHistoryService().createHistoricActivityInstanceQuery()
.processInstanceId(processInstanceId)
.activityType("subProcess")
.list()
.stream()
.map(HistoricActivityInstance::getExecutionId)
.filter(execId -> !processInstanceId.equals(execId))
.collect(Collectors.toList());
// Set variable values
for (String subProcessExecutionId : subProcessExecutionIds) {
engineServices.getRuntimeService().setVariables(subProcessExecutionId, Variables.createVariables()
.putValue(VariableConstants.SHIPMENT_DATE, date)
.putValue(VariableConstants.SHIPMENT_COMMENT, comment)
.putValue(VariableConstants.SHIPMENT_REFERENCE, reference));
}
}
I’m not sure I understand the question. What values are you trying to store? Are there “common” values between multiple sub-processes (e.g. SHIPMENT_REFERENCE) such that in a “shared” process variable environment, one sub-process might overwrite that value making it incorrect for another sub-process?
Are these true sub-processes or are they “child” processes invoked through a call activity?
All that said, one way to keep “track” of variables in multiple, possibly similar sub-processes would be to create a JSON object in the “parent” process. This object would have objects representing each sub-process and within that object would be the variables particular to that sub-process. Something like this:
{
"subprocess_1": {
"SHIPMENT_DATE": "2017-09-26 08:00:00",
"SHIPMENT_COMMENT" : "First part of the order",
"SHIPMENT_REFERENCE" : "1234546ABC"
},
"subprocess_2": {
"SHIPMENT_DATE": "2017-09-27 14:00:00",
"SHIPMENT_COMMENT" : "Second part of the order",
"SHIPMENT_REFERENCE" : "456789XYZ"
},
"subprocess_3": {
"SHIPMENT_DATE": "2017-09-28 13:15:00",
"SHIPMENT_COMMENT" : "Final part of the order",
"SHIPMENT_REFERENCE" : "098765FGH"
}
}
You can then use Spin (JSONPath) to extract the appropriate values as needed. The key “subprocess_X” could be substituted with a process instance ID or other value that can be programmatically derived.
Yes so I have one of these things that loop over a collection and they run with a different executionId.
The scope of these shipment variables is different, so for every subProcess task, I can set a different variable value. It’s not a global process variable. Given this, storing it all in one object is not required for me.