Which "Execution Listener Type" can be used in order to call a Delegation Code from a Script Task?


#1

Hi there.

I would like to call a Delegation Code (to convert a created “JSON” variable into a “String”) like this one here:

ObjectMapper mapper = new ObjectMapper();
        String jsonStringMyVariable = mapper.writeValueAsString(execution.getVariable(myJSONVariable));
        execution.setVariable("jsonStringMyVariable", jsonStringMyVariable);

My question is which “Execution Listener Type” can I use in order to call the above code from a Script Task in Modeler ?

Thanks in advance,
Steve


#2

ExecutionListeners can be configured to any of the event type “Start” and “End” execution of the activity.

Both event type supports script execution.


#3

Hi @aravindhrs and thanks for your feedback. :slightly_smiling_face:

I meant which Listener Type (e.x. Java Class, Delegate Expression, Expression, Script) must I use in order to call the above Delegation Code in the Script Task, not the Event Type (Start, End). :wink:

Steve


#4

Try to use Listener Type = Script and Script Type = javascript
like this:


#5

You can use ListenerType=“Script”. You can use either “javascript” or “groovy” as script format. In order to use groovy script you have to make sure to include groovy dependencies in your classpath. Camunda will not include any groovy related dependencies


#6

Hi @volodya327 and @aravindhrs.

Thanks again both of you for your feedback.

Ok, I will try it with javascript as Script Format, because I’m not familiar with “groovy”.
In my case, the created (via camForm function) json variable is called “product”.

So, I suppose that the above script will be written as following:

ObjectMapper mapper = new ObjectMapper();
        String jsonStringMyVariable = 
mapper.writeValueAsString(execution.getVariable(product));
       execution.setVariable("jsonStringMyVariable", jsonStringMyVariable);  

Is it ok?

Steve


#7

Hi again @volodya327 and @aravindhrs.

I tried to execute the above script from my Script Task in Modeler but for some reason, when I submit my 1st User Task form, an Open Incident (with the following stacktrace http://localhost:8080/camunda/api/engine/engine/default/job/bcf79b99-f551-11e8-997d-bc7737358152/stacktrace) is displayed in my Cockpit…

Do you know why does this happen? :thinking:

Thanks a lot,
Steve


#8

this piece of code doesn’t belong to javascript. It’s purely java. Please try javascript code when "scriptformat=javascript"


#9

Hi @aravindhrs.

Ok, I understood. Do you know if I can simply set the Script Format to “java” instead of “javascript” inside the Execution Listener of Script Task?

Thanks,
Steve


#10

it wont work like that. you can use execution listener type as java. java code will not work with javascript or by changing script=java.


#11

Hi @aravindhrs.

So, I must work with “Java Class” as Listener Type.
I suppose that in this case, I must create a new Java Class inside the package of Project in the Eclipse and reference its path in the “Java Class value field” of the Execution Listener in Modeler ?

Thanks,
Steve


#12

yes, you’re correct