- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
06-26-2023 10:30 PM - edited 06-26-2023 10:52 PM
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:
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':
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":
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:
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:
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
- 2,641 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Excellent content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Unique way of remembering the structure of script. Thanks for making the understanding easy and relatable. Good thought process.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Please Find the script for scenario 2
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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);
}