- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2024 12:19 PM
Hello everyone! Just had a quick question about a GlideDateTime since I've been fairly new to scripting in ServiceNow.
I have this table called Reviews that has a Glide Date Time field in it called End Date. I have to query this table using a GlideRecord to get records where the End Date is within the last month. Just wondering how that would look like? Thank you for the time.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2024 12:26 PM - edited 10-11-2024 12:28 PM
Here's an example:
var inc = new GlideRecord('incident');
inc.addEncodedQuery('opened_atRELATIVEGT@month@ago@1');
inc.query();
while (inc.next()) {
gs.info('number = ' + inc.number);
}
Go to a list view of desired records. Right-click on the Breadcrumb for search options, and "Copy query" to use in the above. Use end date instead of 'Opened'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2024 12:27 PM
You can manually filter a list view to get the records you are looking for, then right-click on the last filter breadcrumb and Copy Query. This is an encoded query that can be used on a GlideRecord. One option for End Date within the last month is:
gr.addEncodedQuery('end_dateRELATIVELE@month@ago@1');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2024 12:26 PM - edited 10-11-2024 12:28 PM
Here's an example:
var inc = new GlideRecord('incident');
inc.addEncodedQuery('opened_atRELATIVEGT@month@ago@1');
inc.query();
while (inc.next()) {
gs.info('number = ' + inc.number);
}
Go to a list view of desired records. Right-click on the Breadcrumb for search options, and "Copy query" to use in the above. Use end date instead of 'Opened'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2024 12:27 PM
You can manually filter a list view to get the records you are looking for, then right-click on the last filter breadcrumb and Copy Query. This is an encoded query that can be used on a GlideRecord. One option for End Date within the last month is:
gr.addEncodedQuery('end_dateRELATIVELE@month@ago@1');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2024 02:43 PM
Thank you Brad! Last question if possible. After I create a query and get those records, how do I output those records in another table I created called Closed Reviews? Right now I have:
var gr = new GlideRecord('Review Table'){
gr.addEncodedQuery('end_dateRELATIVE@month@ago@1');
gr.query();
while (gr.next()){
?????????
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2024 03:16 AM
You would need a code block like this which maps every field you need from the 'Review Table' to 'Closed Reviews', also making sure that the field types match or are compatible.
var gr = new GlideRecord('Review Table'){
gr.addEncodedQuery('end_dateRELATIVE@month@ago@1');
gr.query();
while (gr.next()) {
var closedRev = new GlideRecord('Closed Reviews');
closedRev.newRecord();
closedRev.u_review_number = gr.number;
closedRev.assigned_to = gr.assigned_to;
closedRev.insert();
}
Of course using the correct table and field names for both tables.