ServiceNow Certification Mock Exams
Click To View Now
SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
How To Script In Flow Designer
How To's

How To Script In Flow Designer

How To Script In Flow Designer (ServiceNow)

Overview

In this post, we’ll break down the basics of scripting in Flow Designer. We’ll review how to create a script in Flow Designer, walk through through a real-world example, and then provide some code snippets that you can refer back to next time that requirement is thrown your way.

How To Create A Script In Flow Designer

You can create a script in several different Flow Actions (i.e. ‘Ask For Approval’ or ‘Update Record'). On the Action, you’ll see a scripting icon next to any field that allows scripting. This icon allows you to write inline scripts to modify values, do transformations, and more.

In the image below, you can see that there are 3 fields with scripting as an option. If you wanted to script a custom short description, you’d click on the icon that is circled:

How To Create A Script In Flow Designer ServiceNow

After clicking the icon, the code editor will display the inline script as shown below:

Inline Script Editor In Flow Designer

Now that you’re in the code editor, you’re ready to start writing your script!

The Basics - How To Retrieve Variables

The inline script in Flow Designer gives you access to the fd_data object. This object can access data from previous actions and allows you to dot-walk to values.

The most common use for scripting in Flow Designer is to retrieve variable values and add them to a short description, update a record, etc.

To get the value of any variable (from the Requested Item / catalog item variables), you can dot-walk to retrieve it.

For example, if you wanted to get the values for ‘first_name’ and ‘last_name’ from your catalog item, then you’d do the following:

 // retrieve variable values
var firstName = fd_data.trigger.request_item.variables.first_name;
var lastName = fd_data.trigger.request_item.variables.last_name;

To send the value, transformation, etc. back to the Flow, you simply add a return statement in your script.

For example, if you wanted to set the short description as: “first_name + last_name has submitted a Request”, then you could do the following (building on top of the example above):

 // retrieve variable values
var firstName = fd_data.trigger.request_item.variables.first_name;
var lastName = fd_data.trigger.request_item.variables.last_name;

return firstName + lastName + ' has submitted a Request';

This will set the short description to the value in your return statement.

A cool feature of the inline script editor is that it will actually display a set of choices as you dot-walk. After you enter the dot (a period) after fd_data.trigger, a dropdown will display with options for the next step of the dot-walk. A couple examples are shown below:

Dot-walk in ServiceNow Flow Designer Script
dot-walking example #1

Dot-walk in ServiceNow Flow Designer Script
dot-walking example #2

Real-World Example

The easiest way to learn how to write a Flow Designer script is to actually write one - so let’s get into it!

In our example, we’re going to write a script that will set the short description based on which user was entered into a catalog item. Our catalog item is used to request World Cup tickets.

The short description will read: [name] has requested World Cup Tickets

The item will ask for a name, which is a reference to the User [sys_user] table. If a name is not able to be found, then the user can type the name into a single line text field.

As shown below, when you check the ‘Name not found box’ - the ‘Your name’ field will switch to a single line text field:

For example, if you are looking for ‘Lionel Messi’ and can’t find him in the reference field, then you can enter his name into a single line text field. Since we want his name in the short description regardless of which field was used, we can utilize a script to accomplish this.

We’ll do our scripting in the ‘Short Description’ field on a ‘Create Catalog Task’ action (as shown below):

Inline script editor in Flow Designer

Let’s start by retrieving the variable values for both of the potential name fields, name_reference and name_text.

 // retrieve variable values
var nameReference = fd_data.trigger.request_item.variables.name_reference;
var nameText = fd_data.trigger.request_item.variables.name_text;

Now, let’s add some logic to determine which field was populated. We’ll set a new variable, name, equal to the value that was populated.

 // retrieve variable values
var nameReference = fd_data.trigger.request_item.variables.name_reference;
var nameText = fd_data.trigger.request_item.variables.name_text;

// declare the 'name' variable
var name;

// determine which field was populated, set it equal to 'name'
if (nameReference) {
    name = nameReference;
} else {
    name = nameText;
}

Let’s recap what happened above. If the user was able to find their desired name in the reference field (name_reference), then we will use that value. However, if the user had to manually enter a name into the single line text field (name_text), then we will use that value.

We can move onto the last part of the script, which will return the short description. Remember, we want it to say: [name] has requested World Cup Tickets. And more importantly, we want the [name] to be populated regardless of which name field the user entered.

 // retrieve variable values
var nameReference = fd_data.trigger.request_item.variables.name_reference;
var nameText = fd_data.trigger.request_item.variables.name_text;

// declare the 'name' variable
var name;

// determine which field was populated, set it equal to 'name'
if (nameReference) {
    name = nameReference;
} else {
    name = nameText;
}

// return the short description
return name + ' has requested World Cup Tickets'

Let’s assume that the user entered ‘Lionel Messi’ into the single line text field (name_text). The short description will be populated as shown below:

Short Description Example on Requested Item

And that’s it! We’ve now walked through a full example of how you can utilize the inline script editor in Flow Designer to add logic to your Flow.

A quick note on a potential troubleshooting issue: if you notice that the Flow is not triggering from a script that you're working on, it may be due to setWorkflow. If setWorkflow is set to true in a script, it will prevent the Flow (among other things) from triggering.

Putting It All Together

In this post, we reviewed the basics of Flow Designer scripting, the fd_data object and it’s dot-walking features, as well as a real-world example of writing a script.

The goal of Flow Designer is to minimize scripting, however, sometimes you just need something that no-code can’t offer! This is where it pays to have a base knowledge of scripting in Flow Designer, and we hope this post helped get you there.

For more info and to see the official docs, you can check out inline scripts on ServiceNow’s official docs. Thanks for reading!

Looking for something else? Check out our other posts below!

Interested in making more money as a ServiceNow developer? We're here to help. We're launching a set of courses, 1:1 mentorship and a community to help you increase your earnings.

To learn more, sign up here: Make More Money As A ServiceNow Developer

Snowycode team
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Read more