Glide record query to skip insertion of one record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 08:52 PM
Hi Developers,
I am writing a Glide Record Query which checks for a particular Serial Number in Table 1 and if a match is found it inserts all the records that exists for that serial number in another Table 2 except the top record when sorted by sys_created_on field.
Bit confused, how it can be achieved by using glide records.
Mainly how do I skip insertion of top record ?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 09:28 PM
Maybe use getRowNumber() after you sort with sys created by. If getRowNumber() != '0' then insert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 09:32 PM
You could use the GlideRecord chooseWindow() method to start at 1 instead of 0. Just make sure to also use the orderBy() to sort on sys_created_on so that it skips the record that you care about. Unfortunately, chooseWindow() requires a second parameter to end on, but you can use a number larger than your actual record count without issue.
gr.chooseWindow(1, 10000);
gr.orderBy('sys_created_on');
chooseWindow(): https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_ScopedGlideRecordChooseWindow_Numbe...
orderBy(): https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_ScopedGlideRecordOrderBy_String
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 09:52 PM
Hi Anirudh,
try using chooseWindow() method of GlideRecord class
check sample link below
https://snprotips.com/blog/2016/9/1/gliderecord-pagination-page-through-records-with-ease
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2019 10:05 PM
Hi ,
I have made sample code . Hope this will give you an idea
var LEN = '';
var gr = new GlideRecord('table1'); // table 1 where you will be matching
gr.addQuery('serial_number','ABCD');
gr.query();
while (gr.next()) {
// if found then that much amount of times insert should be done except first record
something(); // function for getting the total number of record in table2 and this will be called only once.
var something = (function(){
var executed = false;
return function(){
if(!executed)
{
executed =true;
var tab2 = new GlideRecord('table2');
tab2.query();
if(table2.next())
{
LEN= tab2.getRowCount();
}
}
};
})();
var table2 = new GlideRecord('table2');
table2.chooseWindow(1,LEN);
table2.intialize();
table2.setValue('short_description','this Serial Number inserted');
// For setting the limit from 2 second record to the end of the list
table2.insert();
}
Mark my ANSWER as CORRECT n also HELPFUL if it helps