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

Code Smart with Script Include - How to read the Docs - Scripting Part 1 (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 classto 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 atDocsorHere). Today we will be creating aUtilities Script Include to help you code smart.

I was inspired to find this capability of script include after reading@KalaiarasanPushpanathan’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 isitself a way of coding smarter. Wondering why?

  • Script Includes areonly loaded on request
  • Script Includes arereusableserver-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 onsome query. Theshortest 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();

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

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);

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

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 toSystem Definitions>Script Includes and click ‘New’

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

(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@MouliPraneethin one of my projects. E.g. ‘ssru‘ forBusiness Rules)

Step 2: Give it a unique name

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

Step 3 : Change the ‘Accessible from’ to ‘All application scopes’ so that it can be accessed from any application scope.

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

Step 4 : Delete the ‘initialize’ function defined in line 3 & 4.

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

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();

returngr.getRowCount();

};

SmartSI.getRecords = function(table, query) {

var gr = new GlideRecord(table);

gr.addEncodedQuery(query);

gr.query();

return gr;

};

SmartSI.getRecList = function(table, query) {

varrecList = [];

var gr = new GlideRecord(table);

gr.addEncodedQuery(query);

gr.query();

while (gr.next()) {

recList.push(gr.getUniqueValue());

}

returnrecList.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

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

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.

Code Smart with Script Include - How to read the Docs - Scripting Part 1 (2024)
Top Articles
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 6715

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.