The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Hemanth M1
Giga Sage
Giga Sage

We often hear about and occasionally implement this feature in ServiceNow. I would like to explain it today with a practical example 😎.

 

What is Script Action:

A Script Action in ServiceNow is a server-side script that gets executed in response to specific events within the platform

You can call it from server-side scripting supported configurations (ex: Business Rules, Workflow scripts, fix scripts, UI actions, flow designer scripts etc…)

 

When to use it?:

Use Script Actions when you need to trigger specific actions automatically in response to events, such as record changes or system events. They are ideal for scenarios where consistent and timely execution of tasks is critical, such as sending notifications or updating related records.

 

Example:

Let's say your organization or client has set up governance around the update set migration process, and it should happen only through update source process, not the import/export method.

 

If anyone or any team deviates and commits update sets through the import/export process, Update committed update set description and Notify the System Admins with a notification.

 

How is it done?? lets see!

 

Step 1: Create an event in the registry table under System Policy > Events

 

HemanthM1_0-1735316475496.png

 

 

Step 2:Set an appropriate name and select the remote update table name "sys_remote_update_set"

 

HemanthM1_1-1735316655180.png

 

 

3)Create a business rule to track the update set commit and call the event created in the first step.

 

HemanthM1_2-1735316927404.png

 

HemanthM1_0-1735363386050.png

 

 

Script :

 

 

 

 

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    gs.eventQueue('update_set_commit_without_source', current, current.getUniqueValue()); //Trigger the script action to update the description of the committed remote update. This will also send an email to the system admins to notify them that the update set has been committed without a source.

})(current, previous);

 

 

 

 

 

4)Create a Script Action as below:

System Policy > Script Actions

 

HemanthM1_4-1735317354289.png

 

Create a script action by selecting the event name created in step 1 and add the logic below to get details from the event and update the committed update set with a description by appending "This has been committed without a source."

 

Make sure you activate the script action because it is unchecked by default.

 

HemanthM1_1-1735363470402.png

 

Script: 

 

 

 

 

var remoteUpdatesetSysid = event.parm1; //get commitetd remote update set sys_id

var remoteUpdate = new GlideRecord("sys_remote_update_set");
remoteUpdate.addQuery("sys_id", remoteUpdatesetSysid);
remoteUpdate.query();
while (remoteUpdate.next()) {
	gs.eventQueue('notify_update_set_commit_without_source', remoteUpdate); //Send a notification to the admin team

    remoteUpdate.setValue("description", remoteUpdate.getValue('description')+"\n"+"This has been committed without a source by :"+" "+remoteUpdate.getValue('sys_updated_by')); //update the committed description to track 
    remoteUpdate.update();
}

 

 

 

 

 

5)Create a new event for notification (refer step 1) and use the event name to call the notification

 

Call the event

 

HemanthM1_2-1735363656136.png

 

 

Add subject and email details:

 

HemanthM1_3-1735363709790.png

 

Add admin groups to be notified, or you can add individual admins or use script logic if required!

HemanthM1_4-1735363748292.png

 

 

 

Let's see the output:

1)Retrieving an update set through the import/export method.

 

HemanthM1_0-1735369925047.png

 

 

2)Lets preview and commit the update set

 

HemanthM1_1-1735370019309.png

 

 

Description has been updated 😍

 

HemanthM1_2-1735370123206.png

 

 

Notification: 😍

To notify system admins!

 

HemanthM1_3-1735370295914.png

 

 

HemanthM1_4-1735370399227.png

 

 

You can debate that we can also use an Async Business Rule. Yes, you're right, However, I am trying to explain how a Script Action is decoupled from records and can perform various actions with an example and how to set it up.

 

Here are some of the differences between an Async Business Rule and a Script Action

 

Feature Script Action Async Business Rule
Trigger Event Based Record Operations (Insert, Update, Delete and Query)
Execution Context

Runs asynchronously via the event queue.

Runs asynchronously after the transaction completes.
Use case Background tasks, Integrating with external systems during data sync/update.

Post-transaction updates, related record processing.

Scope Decoupled from records Tied to specific table and record operations

 

Thank you,

Hemanth

ServiceNow MVP 2024 and ServiceNow Community Rising Star 2022 & 2023

 

My other articles :

 

A Quick Guide to Adding a Wizard Section to a Catalog Item in Catalog Builder! 

 

Set different “From” and “Reply To” emails for the Native and Flow designer Emails/Notifications. 

 

Read .CSV formatted files in ServiceNow 

 

Automate Assessment/Survey Testing with ATF(Including Multiple Option,Attachments &Reference fields) 

 

Set up interactive filter on UI builder (for data visualization reports and List records) 

 

Set Up "Step Based Request Fulfillment" for Catalog Items 

 

Need to know about Schedules and Define them in SLAs 

 

My ServiceNow Share Projects:

 

Return Assessment Feature in ServiceNow 

 

Add signature and update fields to a fillable PDF document

 

Get Record URL action in the flow designer (Use it in the Send Email action) 

 

Domain Separation: Alert Developers/Users when open a configuration record (ex: Business rules) in d... 

 

Custom Generic Flow Action to Create Assessment 

 

 

Comments
Uncle Rob
Kilo Patron

Great work!  I've used Script Actions in the past when Process A can launch Process B, but only sometimes.
Process B is really its own thing, so I don't want massive branches or subflows in Process A.
Its also fun to note that events can be catalysts for both Script Actions and Notifications.  
So in your example you could take the extra event out of the script, and have your notification fire on the same event as the Script Action.

 

Now if only Flow Designer could have events as triggers... 
React-SaganSmile.gif

Uncle Rob
Kilo Patron

ANOTHER EXAMPLE  of event based process launching.
A past employer had an IT coms team responsible for talking to the rest of the organization with respect to "high visibility STUFF".  Think major incidents, high impact problems, emergency or failed changes, big upcoming releases, project milestones etc.  A wide variety of task types could trigger a com-team task.

Each of those processes could invoke a "com.team.task.needed" event, feed important info via parameters, and have script action create the necessary task for com-team.

"But Uncle Rob, if I could launch the event in code, I can create the task there too"
Yes, BUT that's 10+ lines of code every single time you want to do it (I listed 5 scenarios).
VS, write the com-task builder code once, and feed it parameters from an event that can launch from multiple sources.

Hemanth M1
Giga Sage
Giga Sage

Hi @Uncle Rob  Thank you!

 

That’s correct I could have used the same event to trigger the notification but I thought

1)let’s do everything in the script action.

2)Fire the notification little late in the action 🤩 (a few milliseconds)

Abhijeet Pawar
Tera Contributor

Hello @Hemanth M1 

Is there any way to roll back an action performed by a Script Action?
I could not find any relevant information in the ServiceNow documentation.

Could you please guide me on this?

Thank you.

Philip Green
Mega Guru

This is extremely informative and helpful - thank you.

Sameer Fayaz1
Tera Contributor

This is really helpful and informative. Thanks for sharing.

Version history
Last update:
‎12-27-2024 11:20 PM
Updated by:
Contributors