Email body variables in Flow

Published Categorized as ServiceNow Tagged , , , ,

While we are gradually transitioning from standard inbound actions to inbound flows, we are losing access to certain native functionalities that are often easier to script in ServiceNow. Achieving these functionalities in a low-code environment like inbound flows can sometimes be challenging.

Scripted inbound actions

A native feature of scripted inbound actions is the ability to access email body variables. For example, if an email body contains this line –

First name:Dinesh

The inbound email script automatically creates the variable ’email.body.first_name’ with the value of ‘Dinesh’. Such variables can be utilised to perform logical actions, like –

if(email.body.first_name != undefined) {
   current.first_name = email.body.first_name;
}

Inbound email flow

Flows provide certain data pills when an email is received, which are excellent for performing drag-and-drop low-code operations. However, if we need to map a specific value from the email body directly to a field on the record, these data pills are not helpful.

Workaround

We can utilise regular expressions (regex) to achieve the same functionality as email body variables. This approach requires some scripting but can be turned into a reusable component, such as a function or a flow action.

Regex

Taking the previous example, if an email body contains this line –

First name:Dinesh

We can use regex to extract the key:value pair.

body.match(/First name:(.+)/);

Output –

First name:Dinesh,Dinesh

Next, splitting by the comma can provide the exact value we need. Here is a reusable code snippet that can be converted into a function or flow action –

Copyable snippet –

/* 'body' contains the entire email html */
var key = body.match(/key:(.+)/);
/* performing a regex match to extract the
value from the key: value pair */

if (key) {
    key = key.toString().split(',')[1].trim();
    key = key.replace(/(<([^>]+)>)/gi, "");
    /* do something */
} else {
    /* value not found */
}

Next steps

Test out all edge case scenarios thoroughly after implementing the logic. Share among your peers.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses User Verification plugin to reduce spam. See how your comment data is processed.