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

# Dataset trigger

## Introduction

The trigger defines procedures that are implicitly and automatically executed when some operations are performed on the dataset. The trigger can be executed either just before or just after the event (creations, updates, or duplications) .

The DSL (Domain Specific Language) is used to define a dataset trigger. For more details on DSL, see the Reference .

To create a dataset trigger, use the Data Model Assistant (DMA) .

To edit, modify and publish a script of the dataset trigger, use the EBX® IDE that provides contextual code completion.

## Initial script

### Introduction

When a dataset trigger is first edited in EBX® IDE, an initial script with the correct definition of the exported procedure is provided.

An initial script of a dataset trigger has following structure:

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

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

The following script is a an example of before modify trigger:

    // this trigger updates root fields before modifying the dataset.
    export procedure onBeforeModify()
    begin
    _ebx.root.fieldA:='xx';
    _ebx.root.fieldB:='yy';
    end

### Exported procedures

An exported procedure can be called directly by EBX®. Only one exported procedure is allowed per script in a dataset trigger.

The signatures of dataset trigger procedures are the following:

- onBeforeCreate() : is called before the creation of a dataset.

- onAfterCreate() : is called after the creation of a dataset.

- onBeforeModify() : is called before the modification of a dataset.

- onAfterModify() : is called after the modification of a dataset.

- onBeforeDelete() : is called before deleting a dataset.

- onAfterDelete() : is called after deleting a dataset.

- onAfterDuplicate() : is called after duplicating a dataset.

Note: The exported procedure signature (name) depends on the trigger’s type and should not be changed.

## Predefined variables

### Introduction

Predefined variables allow access to the context of a dataset trigger. The following contextual variables are specific to the dataset trigger. For more details on common predefined variables, see the Reference .

### Session

The predefined variable _ebx.session provides access to information on the current session. This contextual variable is constant and of an immutable complex type. This variable is also available in the context of a table trigger.

It has the following fields:

Name | Type | Description
--- | --- | ---
userId | string | The identifier of the current user
userEmail | string | The email of the current user
locale | string | The locale of the current session

Example:

    // this triggers logs user identifier of the current session.
    uses core.log as log;
    export procedure onBeforeCreate()
    begin
    log.info(' User id : ' | _ebx.session.userId);
    end

### originalDataset

The predefined variable _ebx.originalDataset provides access to information of the original dataset. It is available only for the trigger after duplicate.

This variable has the following fields:

Name | Type | Description
--- | --- | ---
name | string | The name of the original dataset
dataspace | dataspace | The dataspace of the dataset

Example:

    // this triggers logs the original and the current of dataset after the duplicate operation.
    uses core.log as log;
    export procedure onAfterDuplicate()
    begin
    var message := \n Original dataset : ' | _ebx.originalDataset.name;
    message |= '\n Current dataset : ' | _ebx.dataset.name;
    log.info(message);
    end

### Transaction

The transaction is needed to perform operations on the dataset such as creation, delete and duplicate. The predefined variable _ebx.transaction provides a read-only access to the current transaction of the dataset trigger. It is available only for dataset trigger afterCreate , afterModify , afterDelete and afterDuplicate . This variable is constant and of an immutable complex type.

Example:

The following script is a an example of after duplicate trigger:

    uses core.data as d;
    uses core.complex as c;
    // The trigger deletes a record from a table after duplicating the dataset.
    export procedure onAfterDuplicate()
    begin
    var table := d.findTable<.table>(_ebx.dataset);

    // Creates the primary key.
    var pk := c.primaryKeyOf<.table>();
    pk.id := '1';

    // Deletes the record using the primary key.
    d.deleteRecordByPrimaryKey(_ebx.transaction, table, pk);

    end

The following script is a an example of after create trigger:

    // this trigger updates fields values after creating the dataset.
    uses core.data as d;
    export procedure onAfterCreate()
    begin
    var field:=d.lookupDatasetFieldForUpdate<typeof _ebx.root>(_ebx.transaction,_ebx.dataset);
    field.fieldA:='A';
    field.fieldB:='B';
    d.saveField(field);
    end

Documentation > Developer Guide > EBX® Script > Usage