- 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:37 PM - edited 10-11-2024 12:38 PM
More resources for you:
A course: https://developer.servicenow.com/dev.do#!/learn/courses/washingtondc#scripting-in-servicenow
Look at OOG examples in Business Rule and Script Includes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2024 12:57 PM
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!