Tuesday, July 14, 2020

Code Smart with Script Include - How to read the Docs - Scripting Part 1

Hi Developers,
Welcome to another article in the series and the first on the scripting. Today we will talk about how to code smarter with Script Includes.
Script Includes defines a function or class to be reusable server-side script logic that execute only when explicitly called by other scripts.
Let us talk about Script Includes later someday (You still can read about them at Docs or Here). Today we will be creating a Utilities Script Include to help you code smart.
I was inspired to find this capability of script include after reading @Kalaiarasan Pushpanathan's blog post 'Unlearn Series - Common Library Functions - Part1'
You might be thinking how can we code smart using Script Includes?
First of all, using Script Includes is itself a way of coding smarter. Wondering why?
  • Script Includes are only loaded on request
  • Script Includes are reusable server-side script logic
We will be using the same capability of it for our server side scripts to make it quick and smart. How?
Let me give you some examples,
If you have a requirement, let's say, to update the Incident state based on some query. The shortest GlideRecord query will look somewhat like this :
var gr = new GlideRecord("incident");
gr.addEncodedQuery("active=true^caller_id=77ad8176731313005754660c4cf6a7de");
gr.setValue('state',2);
gr.updateMultiple();
And if you have to update another set of records to different values for different query condition on different table, you have to write it again!
But if we use the capability of re-usability of Script Includes we don't need to re-write all of the code. And In fact, your code will be reduced to a single line :
MySI.updateList('incident','active=true^caller_id=77ad8176731313005754660c4cf6a7de','state',2);
You can change the parameters (table, query, field, value) to different values to use it as per your requirement.
How to achieve it? I will demonstrate you how to create a smart Script Includes with some simple examples, Feel free to create your own with your own piece of code, and don't forget to share your new findings here on ServiceNow Community.
Step 1 : Navigate to System Definitions > Script Includes and click 'New'
(Tip : Observe the way we filter the applications in navigator, the last two letters of first word and first two letters of second word. It is a shortcut to filter applications I learned from my lead @Mouli Praneeth in one of my projects. E.g. 'ss ru' for Business Rules)

Step 2: Give it a unique name

Step 3 : Change the '
Step 4 : Delete the 'initialize' function defined in line 3 & 4.
Step 5 : Paste the following piece of code after Line 1 and click 'Submit'.
//My Smart SI - Start

SmartSI.getRecCount = function(table, query) {
var gr = new GlideRecord(table);
gr.addEncodedQuery(query);
gr.query();

return gr.getRowCount();
};

SmartSI.getRecords = function(table, query) {
var gr = new GlideRecord(table);
gr.addEncodedQuery(query);
gr.query();

return gr;
};

SmartSI.getRecList = function(table, query) {
var recList = [];
var gr = new GlideRecord(table);
gr.addEncodedQuery(query);
gr.query();
while (gr.next()) {
recList.push(gr.getUniqueValue());
}

return recList.toString();
};

SmartSI.updateList = function(table, query, field, value) {

var gr = new GlideRecord(table);
gr.addEncodedQuery(query);
gr.setValue(field, value);
gr.updateMultiple();

return '';
};

SmartSI.deleteList = function(table, query) {

var gr = new GlideRecord(table);
gr.addEncodedQuery(query);
gr.deleteMultiple();

return '';
};

//My Smart SI - End
Hurray, You are now ready to test your Script Include. Check whether executing following script in Scripts - background deletes all inactive problem records? :
SmartSI.deleteList('problem', 'active=false');
Now you are all set to modify your working Script Include with your own reusable functions and code smarter.
Thank You.
Vishal Ingle
ServiceNow Certified System Administrator
DxSherpa Technologies Pvt. Ltd.
Maharashtra, IN.

No comments:

Post a Comment