Using the JavaScript service

Using the JavaScript service is similar to creating a service, but is somewhat limited. As the name implies, it allows you to execute JavaScript code, and can be used in a series of scenarios. In every case, you have access to the inbound message using the “message” variable, along with every other Flow variable. Please note that this service requires the inbound message to be a JSON object.

Consider using the JavaScript service when you want to:

  • Update a message
  • Create a new message
  • Update flow variables
  • Save message for later processing
  • Set routing conditions
  • Special service messages types
  • Update a message

As with any JSON object the inbound message can be manipulated using JavaScript. If you for instance would like to update a message containing the fields firstName and lastName with full name, you would write:

message.fullName = message.firstName + ' ' + message.lastName;

Create a new message

Creating a new message is usually about transforming the inbound message to a new message. In the example below, we’re creating a new message called destination from the original message.

var source = message; // Build the destination message

var destination = {
    name: prefixName(source),
    children: function () {
        var list = [];
        source.children.forEach(function (child) {
            list.push({
                name: prefixName(child)
            });
        });
        return list;
    }(source)
};

message = destination;

// Function used to prefix names with mr, mrs or ms
// based on the persons sex and marital status
function prefixName(person) {
    var prefix = 'mr. ';
    if (person.sex == 'Female') {
        prefix = (person.maritalStatus == 'Married' ? 'mrs. ' : 'ms. ');
    }
    return prefix + person.firstName + ' ' + person.lastName;
}

The original message looked like this:

{
    "firstName": "Jane",
    "lastName": "Smith",
    "sex": "female",
    "maritalStatus": "Married",
    "children": [
      {
          "firstName": "Pontus",
          "lastName": "Smith",
          "sex": "male",
          "maritalStatus": "Single",
          "children": [
          ]
      },
      {
          "firstName": "Matilda",
          "lastName": "Smith",
          "sex": "female",
          "maritalStatus": "Single",
          "children": [
          ]
      }
    ]
}

While the new message looked something like this:

{
    "name": "mrs. Jane Smith",
    "children": [
      {
          "name": "mr. Pontus Smith"
      },
      {
          "name": "ms. Matilda Smith"
      }
    ]
}

Please note that the outbound message is set using:

Update flow variables

Any variables defined in the flow can be read or updated in the JavaScript service. You might for instance want to set the outbound file name in the script:

// flow variable

outboundFileName = message.orderId + '.txt'

Save message for later processing

You can also save the message to a variable for later usage:

customerlMessage = message;

This can come in handy in scenarios where after receiving the first message, you’d like to call out oto another service to get some additional information which you’d like to add to the message:

customerMessage.address = message;

Set routing conditions

When setting routing conditions, you use the same script editor, but the result is evaluated differently. After the routing condition has been executed, the service looks at route variable to determine a Boolean value whether or not to continue.

route = message.customerType == 'VIP';

Special service messages types

TIP! The message types below are available as snippets, by typing "sql" + CTRL + space.

Some services require a special type of message, such as all database services (MSSQL, MySQL, and Oracle).

For SQL databases such as Oracle, MS SQL and MySQL:

Database SELECT message
message = { 'type': 'SELECT', 'command': 'select * from WorkOrders' }; 

Database INSERT message
message = { 'table': 'WorkOrders', 'type': 'INSERT', 'dataRows': message.body } 

Database UPDATE message
message = { 'table': 'WorkOrders', 'type': 'UPDATE', 'dataRows': message.body, 'idColumns': ['Id'] } 

Database DELETE message
message = { 'table': 'workorders', 'type': 'DELETE', 'idValues': [message.header.id], 'idColumns': ['Id'] }

For noSQL databases such as MongoDb:

Database SELECT message
message = {
    'type': 'SELECT',
    'collection': 'collection',
    'query': query
};

Database INSERT message
message = {
    'type': 'INSERT',
    'collection': 'collection',
    'data': message
};
Database UPDATE message
message = {
    'type': 'UPDATE',
    'collection': 'collection',
    'query': query,
    'values': values
};
Database DELETE message
message = {
    'type': 'DELETE',
    'collection': 'collection',
    'query': query
};