Custom Action in ServiceNow Flow Designer

In our previous piece, we have discussed in high-level the fundamentals of Flow Designer in Service Now. We have mentioned that the flow is basically consist of a Trigger and sequence of Actions.

While there are several built-in core actions and steps in ServiceNow Flow Designer, we can build our custom action to execute functions not satisfied by the core actions. Since Flow Designer is designed for process owners to automate a process using natural language, developers could create custom actions for process owners use them without worrying of the codes at the far side.

In this piece, we will attempt to discuss the basic steps in creating a custom action in Flow Designer. The custom action we will attempt to simply automate the duration calculation of dates. This is not to mention that this actions could be written directly in the scripts associated with the table. However, our purpose here is to enable process owners to define process automation without writing a line of code.

Let us start by creating a table with From and To date/time fields, and a Duration string field to display the output of our custom action. We will name our table as “Utility”.

AD705B5D-1051-460F-8048-9780C0E3F1EE.PNG
Open image in new tab to clearly view its content.

Custom Action. A reusable custom basically has three components: Inputs, Action Steps, and Outputs. Now, let us open our Flow Designer and create a new Action, a very basic action just to discuss this piece, and let us name it “Date Duration”.

Action Input. An Action Input is data variable which we will be using in custom action. We will create From and To action inputs and both are Date/Time type.

A7BC2F7D-84D8-4AA4-8374-41FA9F69D899.PNG
Open image in new tab to clearly view its content.

Action Step. An action step is a reusable operation within our action. We will create an Action Step using a Script Step. A script step is as well a composite of input variables, script, and output variables.

Let us create “from” and “to” Input Variables and correspondingly map them to our Action Inputs.

A4A76016-6EE6-4D48-BA21-8BF871A57705.PNG
Open image in new tab to clearly view its content.

Then, we will write our Script as:

(function execute(inputs, outputs) {
   var glideFrom = new GlideDateTime(inputs.from);
   var glideTo = new GlideDateTime(inputs.to);
    
   outputs.duration = GlideDateTime.subtract(glideFrom, glideTo).getDisplayValue();
})(inputs, outputs);

And then, we will create an Output Variable named “duration” that will hold the output of our Script and which will be mapped to our Action Output named “Duration” in the succeeding section.

F8FB080F-CD7C-40DD-B643-26F30200E0CF
Open image in new tab to clearly view its content.

Action Output. An Action Output is a data variable that represents the results of the action and in this piece we will name it “Duration” which is mapped to the “duration” Output Variable we have created earlier.

35F50841-D3F2-4C85-A08C-BCD874A46428
Open image in new tab to clearly view its content.

Flow. Once our Date Duration custom action is published, we will create a Flow that simply automates the duration of given dates, targeting our Utility table as its trigger, and use our Date Duration as one of its actions.

In our Date Duration action, notice that the From and To action inputs are correspondingly mapped to the From and To fields of our Utility we have created earlier and made it as out trigger.

3535D38E-C3F1-4096-AC17-2E6661F68258
Open image in new tab to clearly view its content.

And then, in the succeeding step, we have incorporated an action that updates the Duration column of our Utility table by the Duration action output of our Date Duration action.

1F9710C1-7C00-4F90-B207-49F3EB8A705E.PNG
Open image in new tab to clearly view its content.

And finally is the output.

B40673B7-4C85-46CA-914A-24196D0E46D8.PNG
Open image in new tab to clearly view its content.

What is more? We can create our custom JavaScript class in Script Include and consume it in our Script Step, and with this we have a proper distribution and organization of our codes. And even an integration with other systems using IntegrationHub.

Hope this helps.

Leave a comment