GlideDateTime Question

ndejoya
Tera Contributor

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.

2 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

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'.

 

Screenshot 2024-10-11 152808.png

View solution in original post

Brad Bowman
Kilo Patron
Kilo Patron

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');

 

View solution in original post

6 REPLIES 6

Bert_c1
Kilo Patron

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'.

 

Screenshot 2024-10-11 152808.png

Brad Bowman
Kilo Patron
Kilo Patron

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');

 

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()){

?????????

}

}

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.