Skip to content

Instantly share code, notes, and snippets.

@StephenOTT
Last active October 26, 2020 18:44
Show Gist options
  • Save StephenOTT/e04e3ddc4497049b8aa1f2af17b0a933 to your computer and use it in GitHub Desktop.
Save StephenOTT/e04e3ddc4497049b8aa1f2af17b0a933 to your computer and use it in GitHub Desktop.
Camunda Nashorn Script for getting call activity sub process variables using a script
function getCallActivitiesInstanceVariables(targetActivityId, collectionElementVariableName, ignoreVariables) {
return execution.getProcessEngineServices()
.getHistoryService().createHistoricActivityInstanceQuery()
.processInstanceId(execution.getProcessInstanceId())
.activityId(targetActivityId)
.list()
.stream()
.collect(
java.util.stream.Collectors.toMap(
function (i) {
// Set the key of the map to the called process instance ID
return i.getCalledProcessInstanceId()
},
function (i) {
// For each called process instance ID, get the sub-processes/called activity's variables
// For the return variables remap the objects into Maps with the variable name and value
return execution.getProcessEngineServices()
.getHistoryService()
.createHistoricVariableInstanceQuery()
.processInstanceId(i.getCalledProcessInstanceId()).list().stream()
.filter(function (i) {
return i.getName() != collectionElementVariableName
})
.filter(function (i) {
return !ignoreVariables.contains(i.getName())
})
.collect(java.util.stream.Collectors.toMap(
function (i) {
return i.getName()
},
function (i) {
return i.getValue()
}
))
} // end of function
) // end of toMap
) // end of collect
}
function prependAndFlatten(variables, instanceCodeKey){
for each(var i in variables.entrySet()){
var instanceCode = i.getValue().entrySet().stream()
.filter(function(e){return e.getKey() == instanceCodeKey})
.findFirst().get().getValue()
i.setValue(
i.getValue().entrySet().stream().collect(java.util.stream.Collectors.toMap(
function(e){return instanceCode + '_' + e.getKey() },
function(e){ return e.getValue()}
)).entrySet().stream().filter(function(e){
return !e.getKey().equals(e.getValue() + '_' + instanceCodeKey)
}).collect(java.util.stream.Collectors.toMap(function(e){return e.getKey()},function(e){return e.getValue()}))
)
}
var resultVariables = new java.util.HashMap()
for each(var i in variables.entrySet()){
resultVariables.putAll(i.getValue())
}
return resultVariables
}
var ignoreThese = new java.util.ArrayList()
//ignoreThese.add('key')
var result = getCallActivitiesInstanceVariables('Activity_04vyl5z', 'item', ignoreThese)
execution.setVariables(prependAndFlatten(result, "key"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment