Cloud Software Group, Inc. EBX®
Documentation > Developer Guide > EBX® Script > Usage
Navigation mode Documentation > Developer Guide > EBX® Script > Usage

# Script task

## Introduction

A Workflow model is defined by a sequence of steps. A step may be a script task. The script task is an automatic task that is performed without human user involvement. It is based on the procedural language DSL (Domain Specific Language) to define custom operations. For more information on DSL, see Reference .

When the task is ready to start, the engine will execute the script. Once the script is completed, the task is also be completed.

To create and modify a script task, use the EBX IDE editor that provides contextual code completion.

## Initial script

### Introduction

When a script task is first edited using use the script IDE, EBX® provides an initial script with the correct definition of the exported procedure.

An initial script task has following structure:

    <unit usage statement 1>
    <unit usage statement 2>
    ...
    <unit usage statement N>

    export procedure executeScriptTask()
    begin
      ...
      <statement 1>
      <statement 2>
      ...
    end

The following example is a script of a workflow task that logs a message.

    uses core.log as log;
    export procedure executeScriptTask()
    begin
     log.info('A new script task is executed');
    end

### Exported procedure

An exported procedure can be called directly by EBX®. Only one exported procedure is allowed per script task.

The signature of the script task procedure is the following:

    export procedure executeScriptTask()

This signature can not be changed.

## Predefined variables

### Introduction

Predefined variables allow access to the context of a script task. The following contextual variable _ebx.parameters is specific to script task. It is constant and of an immutable complex type. Note that the common predefined variables of contexts (function field, table trigger) described in Reference can not used in the context of script task.

### _ebx.parameters

The predefined variable _ebx.parameters provides access to the parameters of the script task context. The parameters of the script task can be input and/or output . They are defined by using the script IDE. The predefined variable _ebx.workflow provides access to information about the workflow model like the identifier, the creator, the last modification date, etc. These parameter are read-only.

The parameters of the variable _ebx.parameters have the following properties:

Property | Description
--- | ---
name | the name of the parameter.
type | The type of the parameter can be string, boolean, locale…​
isInput | When this property is checked, the parameter is defined as an input. The parameter is not modifiable.
isOutput | When this property is checked, the parameter is defined as an output. The parameter is readable and modifiable by the script. If the output parameter is not also input , its initial value is null.

The syntax to get the value of an input parameter is :

    var a_value := _ebx.parameters.input_parameter_name;

where input_parameter_name is the name of the input parameter.

The syntax to set the value of an output parameter is :

    _ebx.parameters.output_parameter_name := a_value;

where output_parameter_name is the name of the output parameter.

Examples:

    export procedure executeScriptTask()
    begin
      var id:=_ebx.parameters.id; // input parameter of type string.
      var active:=_ebx.parameters.isActive; // input parameter of type boolean.

      _ebx.parameters.country:='USA'; // output parameter of type string.
      _ebx.parameters.locale:=locale.of('fr','FR'); // output parameter of type locale.
      _ebx.parameters.isActive:= false; // output parameter of type boolean.

      _ebx.parameters.message='Task A :' | _ebx.parameters.message; // input output parameter of type string.
    end

    // This task saves information of a person in the table
    export procedure executeScriptTask()
    begin
      var id:=_ebx.parameters.id;
      var firstName:=_ebx.parameters.firstName;
      var lastName:=_ebx.parameters.lastName;
      _ebx.parameters.fullName := firstName | ' ' | lastName;
      savePerson(id,firstName, lastName,_ebx.task.fullName);
    end

#### _ebx.workflow

The predefined variable _ebx.workflow provides a read-only access to information on the current workflow model. This contextual variable is constant and of an immutable complex type.

It has the following fields:

Name | Type | Description
--- | --- | ---
id | string | The identifier of the workflow model
publicationId | string | The publication identifier of the workflow model
label | message | The label of the workflow model
description | message | The description of the workflow model
creationDate | date | The creation date of the workflow model
lastModificationDate | date | The last modification date of the workflow model

Example:

    // This script task logs information of the current workflow model.
    uses core.log as log;
    export procedure onNewContext()
    begin
    var message := ' Workflow identifier : ' |_ebx.workflow.id;
    message |= '\n Workflow creation date : ' | _ebx.workflow.creationDate;
    message |= '\n Workflow last modification date: ' | _ebx.workflow.lastModificationDate;
    log.info(message);
    end

## Transaction

### Introduction

In the context of script tasks, users may need to perform some actions on a dataspace under a transaction (i.e. update, modify, delete records, etc). Hence, if one or more actions fail, all other actions must back out leaving the state of the dataspace unchanged. The transaction ensures that the integrity state of data is never compromised.

### Execute statement

The syntax to declare a transaction is the following:

    execute transaction transaction_variable_name on dataspace_variable
    begin
      // Transaction block.
    ...
      <statement_1>
      <statement_2>
    ...
    end

- transaction_variable_name is the name of the transaction variable.

- dataspace_variable is the dataspace on which the transaction is executed. It is of type 'dataspaceType'.

Constants defined in encompassing scopes can be accessed by the transaction block, but non-constant variables cannot.

Return statements are not allowed in the transaction block.

Example:

    references model "model.xsd" from module "ebx-test" as schema;
    uses core.data as db;

    export procedure executeScriptTask()
    begin
        var myDataspace := db.lookupDataspace('Bdataspace');

        const id := 'xxx';

        // executes a transaction that creates an new  record in the table
        execute transaction tr on myDataspace
        begin
            var dataset := db.findDataset(tr.dataspace, 'dataset');
            var tableA:=db.findTable<schema.tableA>(dataset);
            var recordA:= db.recordOf(tr, tableA);
            recordA.id:=id; // the 'id' value is declared outside the transaction block. It must be const.
            db.saveRecord(recordA);
        end
    end

Notes

Several transaction statements can be defined per script task. The name of transaction variable must be unique in the current scope otherwise an error will be generated at compile time.

    export procedure executeScriptTask()
    begin
        execute transaction tr_a on myDataspace
        begin
          <statement_a>;
        end

        execute transaction tr_b on myDataspace
        begin
          <statement_b>;
        end
    end

The nested transactions are not allowed in the script task procedure. An error will be raised at compile time.

    export procedure executeScriptTask()
    begin
        execute transaction tr on myDataspace
        begin
          <statement_a>;
          // nested transaction is not allowed here !
          execute transaction tr on myDataspace
          begin
          <statement_b>;
           end
        end
    end

Documentation > Developer Guide > EBX® Script > Usage