Script data and Text data

why do we keep Script & Text both in Output Parameters

what is the purpose of Script dataType in Camunda modeller ?

Here is an example:

outputdata

Note that outputVarName is the value of offerprice Script.

For me, the script is one of the most used type in input output mappings, because I can create Objects of any kind easily, at least easier than filling maps and lists, and it’s much more powerful. These objects can be used by other components without any problem.

I’m not sure if offerprices script is evaluated before beeing assigned to the Text Variable outputVarName, so I never use input or output mapping in this way.

My confusion is when we should be using Script type and when Text type ? any example please.

A script is a output type with a name because scripts last executed functions are returned. Example in JavaScript if you just wrote 2+2 then that eval would be returned.

A script does not need to return something, so you have options. Think of scripts as a way to manipulate data and preform actions.

An example:
Suppose you have to pass the output variable as a calculated map to the rest of the execution.
Output Var Name: ā€œmyMapā€
Output Var Type : ā€œScriptā€
script type: ā€œgroovyā€
body:

return [   
    city: [ name: city_name, country: city_country ],
    products: listProducts1.addAll(listProducts2),
]

This script will save in myMap an map of objects (a list and a map in this case)

You can use this object as you wish in other following scripts/juel expression or java delegates.
For example for an expression :

${myService.processAndSave(myMap)}

or
${myService.saveProduct(myMap.products)}

But this is an example. The possibilities are endless.

@Mizar01

You are using ā€œgroovyā€. Do you need to return in grovy script ? I dont know grrovy .I know javascript.

Say , I write these …

Name: outputVar
Type: Script ,
Script Format : Javascript .
Script Type: Inline script

Script
var inData = execution.getVariable(ā€œinputDataā€);
var someOtherVar= JSON.parse(productType);

what will be returned ?

Is it the only the last variable i.e someOtherVar or both ?

You said …

Does this mean only last executed part i.e someOtherVar will be returned here ?

Please correct me if I’m wrong.

@cofactor, You can do a lot of things. As a groovy script, I don’t know if it works for others, you can return a variable using ā€˜return varName’ or simply using ā€˜varName’ as the last line of the script.

Like you said, the last line of code will be the returning value, but if you put
varName = ā€œsome Valueā€
this line will return null, because it’s an assignment. In groovy this will return the variable itself, so it should work, but I don’t guarantee in other languages. You should do something like one of these:

varName = ā€œsome Valueā€
varName
OR
ā€œsome Valueā€
OR
return ā€œsome Valueā€
OR
varName = ā€œsome Valueā€
return varName

You can also return null and set some execution variables (maybe you don’t use a script for input mapping without a returning value, but theoretically you can).

1 Like

@Mizar01

That was very much helpful.

@cofactor

you would do:

var inData = execution.getVariable(ā€œinputDataā€);
var someOtherVar= JSON.parse(productType);
someOtherVar;

Where line 3 is the return final value. But you do not need to do this in every case. often you are wrapping all of your code in ā€œfunctionsā€ and thus you are using execution.setVariable() rather than the final returned value.

1 Like