# BPMS script task (scriptTask)

Camunda's default option allowing you to create simple, fast scripts without the need to create Java/ScriptCode handlers

Available languages include, for example, Groovy/JavaScript

## Documentation <a href="#zadanieskryptowe-scripttask-camundy-dokumentacja" id="zadanieskryptowe-scripttask-camundy-dokumentacja"></a>

<https://docs.eximeebpms.org/manual/latest/reference/bpmn20/tasks/script-task/>

<https://docs.eximeebpms.org/manual/latest/user-guide/process-engine/scripting/>

## Attaching a ScriptTask to a process <a href="#zadanieskryptowe-scripttask-camundy-podpinaniescripttaskdoprocesu" id="zadanieskryptowe-scripttask-camundy-podpinaniescripttaskdoprocesu"></a>

We choose **Task** and subtype **ScriptTask**:

* Format: Language, e.g. Groovy/JavaScript
* Type: Inline Script
* Script: the script we want to invoke

<figure><img src="https://2112972046-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2CssJT0zIo4SJQLbSZ6l%2Fuploads%2F217mGjq4Hr8TLOYctOYp%2Fimage2024-10-18_12-9-0.png?alt=media&#x26;token=20240be1-9168-46d2-b476-2fb05d78e538" alt="" width="347"><figcaption><p><em><strong>Figure 1.</strong> Example of attaching a ScriptTask in a process</em></p></figcaption></figure>

## Common methods <a href="#zadanieskryptowe-scripttask-camundy-popularnemetody" id="zadanieskryptowe-scripttask-camundy-popularnemetody"></a>

```
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() - retrieves the process ID
```

<details>

<summary>Setting data visible in the Eximee dashboard</summary>

```
    dashboardsParams = [
        "[1]First and last name"             : execution.getVariable("clientFullName"),
        "[3]General type of instruction"     : execution.getVariable("globalCaseTypeName"),
        "[4]Product group"                    : execution.getVariable("caseGroupName"),
        "[5]Instruction"                     : execution.getVariable("caseTypeName"),
        "[13]Last person handling the case:": 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]Method of delivery:"] = "e-mail"
        }
 
execution.setVariable("TASK_DASHBOARD", dashboardsParams);
execution.setVariable("PROCESS_DASHBOARD", dashboardsParams);
```

</details>

<details>

<summary>Handling JSON in JS</summary>

```
const data = execution.getVariable("kgSymulatorData")
const cif = S(data).prop("cif").value()
const creditAmount = S(data).prop("creditAmount").value()
const phone =  S(data).prop("phone").value()
const email = S(data).prop("email").value()
 
 
execution.setVariable("cif", cif);
execution.setVariable("creditAmount", creditAmount);
execution.setVariable("phone", phone);
execution.setVariable("email", email);
```

</details>

<details>

<summary>Converting JSON from string type to Object type</summary>

```
// 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)
```

</details>

More information about JSON: [https://docs.camunda.org/manual/7.7/reference/spin/json/01-reading-json/](https://docs.camunda.org/manual/7.7/reference/spin/json/01-reading-json/?__hstc=218867270.701d355f32e667d247d52b3da539ef04.1729075947404.1729075947404.1729075947404.1&__hssc=218867270.1.1729075947404&__hsfp=456839464#reading-json-properties)
