Open Navigation

Script

The Script connector can be used to add JavaScript functions to your workflows

Overview

The Script connector can be used to add JavaScript functions to your workflows.

The steps involved in using the Script Helper are:

  1. Optionally define input variables which can be pulled in from previous steps in your workflow. The below screenshot shows using $.steps.loop-1.value to pull in the value variable from the loop-1 workflow step.

define-script-variable

  1. Your script must begin with the exports.step = function() line in order to function correctly. From within this you can create and return variables and work with objects. If you wish to use variables pulled in from previous steps, as mentioned in point 1, this should be exports.step = function(input)

  2. In your script, input variables are written in the format input.NAME
    So for example you could say var message_object = input.message; to create an object based on the above screenshot.

  3. What you return in your script will determine what is available for subsequent connector steps to make use of, e.g. return message_object;};

Please see our example worfklow below for a simple demonstration of these points in action.

Note: You can choose from the Operation dropdown whether you want to execute your step synchronously or asynchronously. You should nearly always choose synchronously, unless you're planning to make HTTP requests in your script.

JS Libraries

The Script connector supports some helpful JavaScript libraries which you can use in your script.

lodash
mout
request
moment-timezone

Example

script-example-workflow-2

The above example shows a manually triggered workflow which implements the following steps:

  1. Use a script to create some dummy message values. In a live situation these messages would be pulled in from another connector step (e.g. intercom, ringcentral etc.). Manually creating these values is actually a nice demo of the basics of working with the script connector:

    exports.step = function() {
    return {
    "messages": [
    {"body": "Hello is there anybody there?", "phone_number": "+147438662", "id": 1052332004},
    {"body": "Anyone?", "phone_number": "+147438662", "id": 1052332004},
    {"body": "Ah there you are", "phone_number": "+147438662", "id": 1052332004},
    {"body": "empty", "phone_number": "+147438662", "id": 1052332004}
    ]
    }
    };
  2. Use a Loop Helper to loop through the above. The Loop List Operation can be used to deal with each message one by one.

    loop-messages

    In order to pull in the message for the script we use $.steps.script-1.result.messages
    This is because, if we click on 'Debug' and look at the output from Execute Script (script-1), it is in the format:

    script-1-output

  3. Then for each message that the loop helper processes, we can use another script helper (Check-message-Script(script-2)) to check that the message fulfils certain criteria. We do this by first of all creating a 'message' input variable which pulls the message from Loop Collection (loop-1) with $.steps.loop-1.value:

    message-input-variable

    We have to use value here because the output log of a Loop Connector shows:

    loop-1-output

    The script itself then runs a check on each message fed by the loop, to see if the message itself was empty, in which case reformat it for the database. If it is not empty then it returns the message object unchanged:

    exports.step = function(input) {
    var message_object = input.message;
    if (message_object.body === "empty"){
    return {
    "body": "empty",
    "phone_number": "---",
    "id": "000"
    }
    } else {
    return message_object;
    }
    };
  4. Finally, the result of each loop and message check is fed into a Google Bigquery database (you will need to authenticate the Bigquery connector with your Bigquery account and setup a dataset and table), using the Insert Rows Operation. The Project Id and Dataset Id are specified and then the result of the message is fed in:

    bigquery-input-data

  5. Once the workflow is setup, click 'Run Workflow Now'
    A successful run will then lead to your Bigquery dataset being updated so it will look something like this:

    bigquery-screenshot

    And clicking on the Debug tab of will enable you to view the output logs of each stage of your workflow:

    script-docs-workflow-output-log

Was this article helpful?
Yes
No