The CreatorCon Call for Content is officially open! Get started here.

AnubhavRitolia
Mega Sage

Hi ServiceNow Enthusiasts,

 

This article is specially for people who are beginner in ServiceNow Scripting and facing issue in writing GlideRecord scripts during their projects or interviews.

 

I always imagine list of particular table while writing GlideRecord code so thought to share same strategy with you all. Lets understand it with a real time scenarios:

 

Scenario 1: Print list of top 10 latest created active Incidents whose category is 'Software' and Priority is 'Critical' or 'High'.

 

Step 1: First understand from which table we have to fetch the data. According to the scenario, we are using 'Incident (incident)' table. So lets imagine you have opened Incident table as below:

 

AnubhavRitolia_0-1687799887975.png

 

var inc = new GlideRecord('incident');

 

Step 2: Secondly, check if we have to filter active records or not. Here we need only active records we can imagine adding filter of 'Active=true':

 

AnubhavRitolia_1-1687800049554.png

 

inc.addQuery('active',true);

OR

inc.addActiveQuery(); // more recommended

 

Step 3: Third, check for any additional filter conditions to be added. Here we have condition which we can add in our imaginary table list "Category is Software" AND "Priority is 1-Critical OR Priority is 2-High":

 

AnubhavRitolia_2-1687800376309.png

 

inc.addQuery('category','software');

inc.addQuery('priority','1').addOrCondition('priority',2');

OR

inc.addEncodedQuery("category=software^priority=1^ORpriority=2");

 

NOTE: If we have multiple filter condition, we can better use Encoded Query (addEncodedQuery()) to add all filter conditions at a time as above. We can copy the query from list and use it in parameter directly.

 

Step 4: Fourth, is there any ordering required. Here we have to reorder it based on Latest created record so we have to Sort by 'Created' as Z to A:

 

AnubhavRitolia_3-1687800588889.png

 

inc.orderByDesc('sys_created_on'); // This will sort the records in Descending order of Created field value

 

Step 5: Fifth, See if we have to get any limited number of record. Here we need 10 records only.

inc.setLimit(10); // This will show only 10 records

 

Step 6: Now as we have all the requirements done, we have to 'Run' the Filter:

AnubhavRitolia_4-1687800754700.png

 

inc.query(); // This will run the filter

 

Step 7: Now as we have to fetch any data from each filtered record, we have to iterate each record and get value. Here we have to fetch 'Number' field value for each record so we can use While loop to iterate each record and get field value:

 

while(inc.next()) // This will iterate each record which was filtered above

{

gs.log(inc.number); // This will log the reqiured Incident numbers

}

 

Now our code is ready as below:

 

var inc = new GlideRecord('incident');

inc.addActiveQuery();

inc.addEncodedQuery("category=software^priority=1^ORpriority=2");

inc.orderByDesc('sys_created_on');

inc.setLimit(10);

inc.query();

 

while(inc.next())

{

gs.log(inc.number);

}

 

Now let me give another scenario and you try same strategy to write your GlideRecord script on comments. Or if you have another strategy to implement it, do use and explain it on comment with your script:

 

Scenario 2: Print list of top 5 latest updated active Problems whose category is 'Hardware' and Priority is 'Critical' or 'High' and Assigned To is Logged in User.

 

Please Bookmark this Article 🔖, Share with your friends / colleagues and Mark it as Helpful 👍 if you thought it was helpful.

 

Also feel free to provide your comments/feedback on this Article.

 

Regards,

Anubhav Ritolia

Community Rising Star 2023

LinkedIn Profile

Topmate Profile

 

Comments
meg5
Tera Contributor

Excellent content

Netaji Kadam
Tera Contributor

hi @AnubhavRitolia 

 

really helpful Article.

Thank you for posting.

 

Regards,

Netaji Kadam

Community Alums
Not applicable

Unique way of remembering the structure of script. Thanks for making the understanding easy and relatable. Good thought process.

saicharan3
Tera Expert

Hi @AnubhavRitolia 

 

Please Find the script for scenario 2

 

var inc = new GlideRecord('problem');
inc.addEncodedQuery('category=hardware^priorityIN1,2^assigned_toDYNAMIC90d1921e5f510100a9ad2572f2b477fe');
inc.orderByDesc('sys_updated_on');
inc.setLimit(5);
inc.query();
while(inc.next())
{
    gs.log(inc.number);
}
AnubhavRitolia
Mega Sage

Hi @saicharan3 

 

Looks good but for Logged in user you mentioned particular sys_id '90d1921e5f510100a9ad2572f2b477fe' which I believe may not be right way.

 

You have to use gs.getUserID() function to get logged in user and compared it with Assigned To field.

 

saicharan3
Tera Expert

Hi @AnubhavRitolia 

 

Thanks for the suggestion

schetry
Tera Contributor

scenario 2



var gr = new GlideRecord('problem');
gr.addActiveQuery();
gr.orderByDesc('sys_updated_on');
gr.addEncodedQuery('category=hardware^priority=1^ORpriority=2');
gr.addQuery('assigned_to', gs.getUserID());
gr.setLimit(5);
gr.query();
while(gr.next()){
gs.print(gr.number);
}

Version history
Last update:
‎06-26-2023 10:52 PM
Updated by:
Contributors