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

sadif_raja
Tera Guru

@ndejoya 

To query the `Reviews` table for records where the **End Date** (which is a GlideDateTime field) falls within the last month, you can use the following script with GlideRecord and GlideDateTime.

Here’s how you can approach it:

 Step-by-Step Script

javascript
// Initialize the GlideRecord object for the 'Reviews' table
var gr = new GlideRecord('x_your_table_reviews'); // Replace with the correct table name

// Get the current date and time
var now = new GlideDateTime();

// Get the date for one month ago
var oneMonthAgo = new GlideDateTime();
oneMonthAgo.addMonthsUTC(-1); // Subtract one month from the current date

// Query the table for records where the 'End Date' is between one month ago and now
gr.addQuery('end_date', '>=', oneMonthAgo); // Replace 'end_date' with the correct field name
gr.addQuery('end_date', '<=', now);
gr.query();

// Loop through the results and process them
while (gr.next()) {
gs.info('Review found: ' + gr.getValue('name')); // Replace 'name' with the field you want to display
}
```

 Explanation:
1. **GlideDateTime**: `now` captures the current date and time, and `oneMonthAgo` captures the date one month before the current time.

2. **addQuery()**:
- The first query condition checks for records where `end_date` is greater than or equal to `oneMonthAgo`.
- The second query condition checks for records where `end_date` is less than or equal to `now`.

3. **Query Execution**: `gr.query()` runs the query on the `Reviews` table, and the `while` loop processes each result.

 Notes:
- Replace `'x_your_table_reviews'` with the actual name of your **Reviews** table.
- Replace `'end_date'` with the actual field name for your **End Date** in the table.

This script will retrieve records with an **End Date** that falls within the last month. Let me know if you need any more help!