What is Script Action in ServiceNow with Example | Use and Implementation

When we talk about scripting in ServiceNow then script action in ServiceNow plays an important role like other multiple ServiceNow script for e.g. script include, business rule, background script, UI actions etc. In this article we will share all the required information along with video demonstration to implement the ServiceNow script action with example use case. Below is the table of content which showcase that what all will be shared in this article.

ServiceNow Script Action with Example

Table of Content:

  • What is Script Action in ServiceNow with example?
  • When can we use the Script Actions
  • Call Script Action in Business Rule
  • What is a difference between Script Actions and Script Include?
  • Can we execute script action client side?
  • Video demonstration of implementing Script Action in ServiceNow

What is Script Action in ServiceNow:

Script action are server side script which executes asynchronously when event is processed or triggered from other server side script like business rule, script include, workflow etc. Event is link to script action, means when event is triggered script action scripts gets execute. Like any other server side script (Business rule, script include etc.) in ServiceNow, script actions have access to all the server side glide APIs, script includes, and other server-side resources.

How ServiceNow Script Action works?

Below we have shared the video link which will demonstrate that how we can create and execute the script action script. 

Use of Script action in ServiceNow:

ServiceNow Script action scripts can be used to perform any server side task asynchronously when event is triggered for e.g. modifying configuration item, Closing hundreds of child incident, send large amount of data to external systems, server side script execution etc. Actually using script action not affect system performance and user experience.

Script actions contain scripts which run when an event occurs like:
Approval is cancelled
Change is approved
Problem is assigned etc.

How to Call Script Action in Business Rule?

We can call an event from a business rule and trigger script action.

Three things we have to create:
1. Event Registry
2. Business Rule
3. Script Action

Below is the use case which we have implemented to understand the topic in detail:

Use Case scenario: 

When configuration Item is attached to an incident record, then comment field of that configuration item will get updated with the required information as mentioned below:
  1. This CI is attached to INC#### on #DateandTime#. The Incident state is : ####
  2. This CI is removed from INC#### affected CI field on #DateandTime#. The Incident state at the time of removal is : ####
  3. The INC#### is resolved on #DateandTime#. 

Create Event Registry: 

Name of event should be "update.cmdb_ci_server.comments"

Script written in Business Rule:

Create After Business Rule with execution condition when state and affected ci field value changes. Below is the code written in script section:
(function executeRule(current, previous /*null when async*/) {

var previousCI=previous.u_affected_configuration_item;
var currentCI=current.u_affected_configuration_item;
var changeCIFlag;
if(previousCI==currentCI){
changeCIFlag='false';
}
else{
changeCIFlag='true';
}
gs.eventQueue('update.cmdb_ci_server.comments',current,changeCIFlag,previousCI);

})(current, previous);

Script written in Script Action:

While creating script action do mention the Event Name and write below script:
var changeCIFlag=event.parm1;
var prevCISysID=event.parm2;
var currentCISysID=current.u_affected_configuration_item;
var incNumber=current.number;

updateCIComment(changeCIFlag,prevCISysID);

function updateCIComment(changeCIFlag,prevCISysID){
//When CI is not changed and Incident is not resolved
if(current.state!=6 && changeCIFlag=='false'){
var getAffCI=new GlideRecord('cmdb_ci_server');
getAffCI.initialize();
getAffCI.addQuery('sys_id',currentCISysID);
getAffCI.query();
if(getAffCI.next()){
getAffCI.comments= "This CI is attached to "+incNumber+" on "+gs.nowDateTime()+". The incident state is : "+current.state.getDisplayValue()+"\n"+getAffCI.comments;
getAffCI.update();
}
}
//When CI is changed and Incident is not resolved
if(current.state!=6 && changeCIFlag=='true'){
var getAffCI=new GlideRecord('cmdb_ci_server');
getAffCI.initialize();
getAffCI.addQuery('sys_id',currentCISysID);
getAffCI.query();
//through this we are updating the commnets of existing attach CI
if(getAffCI.next()){
getAffCI.comments= "This CI is attached to "+incNumber+" on "+gs.nowDateTime()+". The incident state is : "+current.state.getDisplayValue()+"\n"+getAffCI.comments;
getAffCI.update();
}
//Through this we are updating the comments of removed affected CI
getAffCI.initialize();
getAffCI.addQuery('sys_id',prevCISysID);
getAffCI.query();
if(getAffCI.next()){
getAffCI.comments= "This CI is removed from "+incNumber+" on "+gs.nowDateTime()+". The incident state at the time of removal is : "+current.state.getDisplayValue()+"\n"+getAffCI.comments;
getAffCI.update();
}
}
//When Incident is resolved
if(current.state==6){
var getAffCI=new GlideRecord('cmdb_ci_server');
getAffCI.initialize();
getAffCI.addQuery('sys_id',currentCISysID);
getAffCI.query();
if(getAffCI.next()){
getAffCI.comments= "This "+incNumber+" is resolved on "+gs.nowDateTime()+"."+"\n"+getAffCI.comments;
getAffCI.update();
}
}
}


Difference between Script Action and Script Include:

Script include holds reusable code which can be called from multiple server side scripts; such as business rule, workflow, script action or another script include itself. Whereas Script action required event to trigger and it is mostly used to perform some transactions or updates asynchronously.

ServiceNow Script Action Implementation Video Link below:


We hope all the information share above related to ServiceNow script action with example will be helpful. If you need information related to any of ServiceNow topic then please let us know and please do share your feedback in below comment box.

No comments:

Thankyou !!!!

Powered by Blogger.