Create Event Triggered Automation Scripts

  1. In FintechOS Studio, go to Main Menu > Advanced > Server Automation Scripts.
  2. Click Insert.
  3. In the Name field, type the script name.
  4. In the Description field type a description of the script logic. The field is optional, but we recommend you to provide a description, so that developers have a clear view on what the script does.
  5. From the Script Type drop-down, select Event triggered.
  6. In the Event drop-down, select the event CRUD operation that triggers the script: Read, Update, Insert, or Delete.
    EventDescription
    ReadTriggers when information from the target entity is read, including by other automation scripts or getByQuery functions. You should be careful not to alter other functionality related to the entity.
    UpdateTriggers when the target entity is updated.
    If an automation script updates another entity, and that entity also has an automation script on update, both scripts will be triggered. If any automation script in the execution chain throws an exception, the entire transaction is roll-backed. That means that every operation is enlisted to the master Ebs Core transaction.
    In every automation script code you have a context variable that holds data about the current scope.
    IMPORTANT!  
    If you write an update script in which you update the same record, it will trigger a recursion. Although the FintechOS Studio has been build to prevent such situations, the transaction will be rolled back after several iterations. There is also a Prevent Recursivity feature that you can enable for such situations.
    InsertTriggers when a new record is added to the target entity.
    DeleteTriggers when a record of the target entity is deleted.
    Can be used in conjunction with:
    • The 'before' stage, otherwise, the automation script tries deleting a record ID which has already been deleted.
    • The 'after insert' script. Do not try to use it 'before insert' as you do not have a record ID yet to relate to.
  7. In the Stage drop-down, choose the stage of event that triggers the automation script:
  8. Stage Description
    Before Executes the code before a read, update, insert or delete event occurs.
    Use cases: To validate information for update/read events and restrict access/filter information for read events.
    After Executes the code after a read, update, insert or delete event occurs. Using the record ID, you can create single or cascading events.
  9. In the Entity drop-down, select the entity triggering the server automation script.
  10. In the Code field, enter the script code (using Server SDK functions). If you want to use Server Automation Script Libraries, you can do so, by calling functions defined in the library. E.g.:
  11. Copy
    log('Client - BeforeUpdate - START - ' + context.Id);
    log('CONTEXT: ' + serialize(context));
    setAdminMode(true);
    if(context.ExecutionDeapth < 2){
        var clientName = isNullOrEmpty(context.Values.AgreementCounterpartyName) ? context.BeforeValues.AgreementCounterpartyName : context.Values.AgreementCounterpartyName;    
        clientName = clientName.trim();
        context.Values.AgreementCounterpartyName = clientName;    
        context.Values.InitialCounterpartyName = clientName;
        var fenergoClientId = isNullOrEmpty(context.Values.FenergoClientId) ? context.BeforeValues.FenergoClientId : context.Values.FenergoClientId;
        var clientId = context.Id;
        if(!isNullOrEmpty(fenergoClientId)){
            var duplicateClient = getByQuery({
                "entity": {
                    "alias": "a",
                    "name": "Client"
                },
            "attributelist": [
                {
                "name": "AgreementCounterpartyName"
                },
                {
                "name": "Clientid"
                }
            ],
        "where": {
            "type": "and",
            "conditionlist": [{
                "first": "a.FenergoClientId",
                "type": "equals",
                "second": "val(" + fenergoClientId + ")"
                },
            {
            "first": "a.Clientid",
            "type": "notequals",
            "second": "val(" + clientId + ")"
            }]
            }
        }
    );
    if(duplicateClient !== null && duplicateClient.length !== 0){
        throwException(getErrorMessage("61111"));
        }
    }
     
        var duplicateClient = getByQuery({
            "entity": {
            "alias": "a",
            "name": "Client"    
        },
        "attributelist": [
            {
            "name": "AgreementCounterpartyName"
            }
        ],
        "where": {
            "type": "and",
            "conditionlist": [{
                "first": "a.AgreementCounterpartyName",
                "type": "equals",
                "second": "val(" + clientName + ")"
            },
            {
            "first": "a.Clientid",
            "type": "notequals",
            "second": "val(" + clientId + ")"
            }]
            }
        }
    );
    if(duplicateClient !== null && duplicateClient.length !== 0){    
        throwException(getErrorMessage("61111"));
        }
    }
    log('Client - BeforeUpdate - END - ' + context.Id);
    HINT  
    To avoid calling the wrong methods and attributes, you can use the ‘$m’ mechanism when writing the code. For more information on how to the mechanism, see Code Snippets Support for JavaScript
     
    If you want to use an automation script library (the one whose functions you appended in the Code field), after saving the automation script, go to the Edit Automation Script page and, in the List of Automation Script Libraries section, click the Insert existing button and select the desired script library by double-clicking it.
  12. To prevent recursive runs of the scripts, tick the Prevent Recursivity checkbox.
  13. Specify if the script is active or disabled. Tick the Disable checkbox to disable the automation script code execution (select it for debugging purposes or for obsolete automation scripts).
  14. Click Save and close to save the automation script.