BPMS script task (scriptTask)

Camunda's default option that allows creating simple, quick scripts without the need to create Java/ScriptCode handlers

Available languages include e.g. Groovy/JavaScript

Documentation

https://docs.camunda.org/manual/7.21/reference/bpmn20/tasks/script-task/

https://docs.camunda.org/manual/7.22/user-guide/process-engine/scripting/

Attaching a ScriptTask to the process

We select Task and subtype ScriptTask:

  • Format: Language e.g. Groovy/JavaScript

  • Type: Inline Script

  • Script: the script we want to invoke

Illustration 1. Example of attaching a ScriptTask in a process

Common methods

execution.getVariable("variableName") - Retrieves a process variable
execution.setVariable("variableName", value) - Sets a process variable
execution.removeVariable("variableName") - Removes a variable from the process
execution.hasVariable("variableName") - Checks whether a variable exists in the process 
execution.getProcessInstanceId() || execution.getId() - gets the process ID
Setting data visible in the Eximee dashboard
    dashboardsParams = [
        "[1]Full name"             : execution.getVariable("clientFullName"),
        "[3]General disposition type"       : execution.getVariable("globalCaseTypeName"),
        "[4]Product group"            : execution.getVariable("caseGroupName"),
        "[5]Disposition"                  : execution.getVariable("caseTypeName"),
        "[13]Last person processing:" : execution.getVariable('executorFirstName') +
        " " +
        execution.getVariable('executorLastName') +
        " (" +
        execution.getVariable('executorId') +
        ")"
        ];
 
    if (execution.getVariable("corpFullName") != null) {
        dashboardsParams["[2]Company"] = execution.getVariable("corpFullName");
        }
 
        dateFormat = "dd.MM.yyyy h:mm"
 
    if (execution.getVariable("startBusinessStatusDate") != null) {
        dashboardsParams["[8]Process start date:"] = execution.getVariable("startBusinessStatusDate").format(dateFormat)
        }
    if (execution.getVariable("tour1BusinessStatusDate") != null) {
        dashboardsParams["[9]Start date of round I:"] = execution.getVariable("tour1BusinessStatusDate").format(dateFormat)
        }
    if (execution.getVariable("tour2BusinessStatusDate") != null) {
        dashboardsParams["[10]Start date of round II:"] = execution.getVariable("tour2BusinessStatusDate").format(dateFormat)
        }
    if (execution.getVariable("tour3BusinessStatusDate") != null) {
        dashboardsParams["[11]Start date of round III:"] = execution.getVariable("tour3BusinessStatusDate").format(dateFormat)
        }
    if (execution.getVariable("endBusinessStatusDate") != null) {
        dashboardsParams["[12]Process end date:"] = execution.getVariable("endBusinessStatusDate").format(dateFormat)
        }
    if (execution.getVariable("sposobWysylki") == "false"){
        dashboardsParams["[14]Shipping method:"] = "e-mail"
        }
 
execution.setVariable("TASK_DASHBOARD", dashboardsParams);
execution.setVariable("PROCESS_DASHBOARD", dashboardsParams);
Handling JSON in JS
var data = execution.getVariable("kgSymulatorData")
//JSON.parse does not work in camunda
var cif = S(data).prop("cif").value()
var creditAmount = S(data).prop("creditAmount").value()
var phone =  S(data).prop("phone").value()
var email = S(data).prop("email").value()
 
 
execution.setVariable("cif", cif);
execution.setVariable("creditAmount", creditAmount);
execution.setVariable("phone", phone);
execution.setVariable("email", email);
Converting JSON stored as a string to an Object type
// The value skpToControl -> [{"skp":"198500"},{"skp":"198700"}]
def raw = execution.getVariable("skpToControl")
def parsed = new groovy.json.JsonSlurper().parseText(raw)
 
def list = parsed.collect { item ->
    def m = [:]
    item.each { k, v -> m[k] = v }
    return m
}
 
execution.setVariable("skpObj", list)

More information about JSON: https://docs.camunda.org/manual/7.7/reference/spin/json/01-reading-json/

Last updated

Was this helpful?