The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Kalaiarasan Pus
Giga Sage

Update: Part-2 of this has been uploaded HERE. This library had a rewrite of all the functions. So please read Part-2  of the blog as well.

This blog is about a share item that I recently uploaded, which I believe will help the community. For the past year or so, I have been actively trying to reduce boilerplate code that I write and create true reusable components. 

The first thing I wanted to address was boilerplate Gliderecord. I am sure, people have their own way of solving this and there might be other share item, which I may have missed during my search. Anyways, I started creating a library function and have been adding functions during past few months. I believe it is at a stage that I can share and keep releasing version updates in future.

You can find the share item related to this HERE.

So what will installing the share item do?

Committing the updateset associated with the share will create a script include and a related unit test for the script. The new script include that is being created is called CommonLibraryFunctions.
 
Let's take a sample scenario where the script will help. Assume you are being asked to check if RITM has any active tasks before performing some action. This is a common ask and one way of implementing the query will look like this.
 
var answer = false;
var ritmSYDID = 'something';
var activeTask = new GlideRecord('sc_task');
activeTask.addEncodedQuery('active=true^request_item=' + ritmSYDID);
activeTask.query();
if (activeTask.hasNext()) {
    answer = true;
}
gs.print(answer);

This is great but as you can see, this is a boilerplate code that gets repeated in different variations across the system. This is where CommonLibraryFunctions will come to your aid. You can rewrite the above script in just few lines as below

var options = {
    table: "sc_task",
    query: "active=true^request_item=" + ritmSYDID,
};
var answer = JSON.parse(new CommonLibraryFunctions().hasRecord(options));
gs.print("hasRecord:"+JSON.stringify(answer));

 

Using the common scripts like this not only promotes code reuse but also helps in limiting your technical debt. It also make it easy to adopt new features or functions. Let's just say ServiceNow comes up with a improved GlideRecord API or a new function in future. You don't have to update 100s of scripts in your system and test all the customization. All you have to do is update one specific function in your library and you are good to go. This will also help if you are the senior developer in team and want to help your junior devs, citizen developers and admins to do their job. This add an additional layer of abstraction over ServiceNow APIs and reduce the clutter.

 

NOTE: If you are interested in extending the project, feel free to contribute using Github repo.

 

 

If you liked the content, please share, click helpful, bookmark or leave your valuable comments.

1 Comment