Hi all,
I am trying to embed a DRD (Literal Expression) in a BPMN Process.
The BPMN Process calls the Literal Expression. I am using JavaScript within the Literal Expression to return a JSONObject which consists of calculations and an output collect array from the first DecisionTable of the DRD.
When I deploy the DRD and evaluate it via REST API, I get the correct result:
[ { "resultVariable": { "type": null, "value": { "rating": 6, "reason": [ "Super", "Not so good" ] }, "valueInfo": null } } ]
However when I use the DMN/DRD in my BPMN Process and try to map the result to a process variable I get following exception:
[ExceptionHandler] (default task-114) org.camunda.bpm.engine.rest.exception.RestException: Cannot submit task form 81dd8c71-a526-11e7-a18c-f45c89cd7b75: Cannot serialize object in variable 'decisionResult': jdk.nashorn.api.scripting.ScriptObjectMirror at org.camunda.bpm.engine.rest.sub.task.impl.TaskResourceImpl.submit(TaskResourceImpl.java:126) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
So my question is:
How can I serialize the result of a JavaScript Literal Expression the best way in the process engine? It works obviously when I convert it to a String, but this is not what I want to do. Also when I convert it to String I have to manually go through the COLLECT Array from the first Table and create my array in JS, otherwise the JSON.stringify method will remove the values.
I will attach the DRD and BPMN file.
When you start the BPMN Process, you can use “Peter” and “Java” as a value in the user task.
When using the DRD directly you can use following request to REST API:
http://localhost:8080/engine-rest/decision-definition/key/scriptDecision/evaluate
{ "variables" : { "name" : { "value" : "Peter", "type" : "String" }, "skill" : { "value" : "Java", "type" : "String" } } }
Best
FelixtestDRDNotWorking.dmn (3.6 KB)
drd.bpmn (3.9 KB)
1 Like
This might be totally off-the-wall, but you may need to convert it to a JSON type. In our DMN tables, we have to go into the DMN definition XML and MANUALLY set the input column “typeRef” value to “json”.
One other thought it so look at your expression language. Sometimes you have to use a specific one to get certain functionality. For example, we do JSONPath evaluations on our input column and the only language we could get to work properly was JUEL.
This may not help in any way, but I thought I’d throw it out there as it may give you an idea.
Michael.
Hi Felix,
Here is a related discussion: Any Benefits of using Spin over Javascript Objects when using javascript Scripting - #3 by thorben
Not sure I understand this. Could you provide a test case that reproduces this?
Cheers,
Thorben
@felix-mueller the issue appears to be with the reasonCode
variable that is used in:
var test = {
'reason': reasonCode,
'rating': newRate
};
The javascript is stripping out the value (likely because its null). Its weird behaviour given that directly through the API you see the response.
But if you manually modify the values to something like:
var test = {
'reason': ["good", "bad"],
'rating': newRate
};
Then the API response gives you expected response when using JSON.stringify(test);
[
{
"resultVariable": {
"type": null,
"value": "{\"reason\":[\"good\",\"bad\"],\"rating\":6}",
"valueInfo": null
}
}
]
So assuming you can get the reasonCode issue resolved, you can do the following:
Update your Script Decision to be:
var newRate = 0;
rating.forEach(function(rate) {
newRate = newRate + rate;
});
var test = {
'reason': reasonCode,
'rating': newRate
};
JSON.stringify(test);
Then in your BPMN, in the Decision Task, create a output Variable with a Javascript script:
S(finaleResult.get('resultVariable').toString());
Decision Tasks Config: Map Decision Result = singleResult
. Returning a hasmap object helps (i believe) to eliminate any possible issues of string 4000 character limit.
And you get:
Hi all,
sorry that it took so long to reply and thank you for your answers!
@mppfor_manu thanks for your thoughts, but I actually only want to use JavaScript so the issue is currently only the serialisation that is done by the engine
@thorben thanks for the link - still I would like that the engine would be able to serialise my result. So I guess I will write a test case soon so I can show my issue inside of a test
@StephenOTT yes, I have noticed that it works this way. I tried this myself, but still I am not sure why I would have to use this workaround as it should be possible to serialise this weird Nashorn Array that I get back.
So I guess the best idea is to provide the unit test that Thorben asked for.
I will get back to you as soon as possible.
Best
Felix