
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 10:02 AM - edited 12-20-2023 10:04 AM
Hey Everyone,
I'm super new with the date/time fields in SNOW. Basically I want to compare two date/time field values that have the same sys_id for a task.
Below is my code for right now.
var incTask = new GlideRecord("task_sla");
incTask.addQuery("task", "task.sys_id");// task.sys_id
incTask.addQuery("has_breached", true);
incTask.query();
incTask.query();
while(incTask.next()){
gs.info("This is the incident number: " + incTask.task.getDisplayValue());
gs.info("This is the breach times: " + incTask.planned_end_time.getDisplayValue());//breach time column
}
What is happening, I'm getting two values for the same task.sys_id for the breach time column. What I want to do is compare those times and choose the earliest/soonest time to breach. This is where I get a little stuff. Does anyone have any suggestions?
This is my log output:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 10:38 AM
@Community Alums : Please try the below code.
var incTask = new GlideRecord("task_sla");
incTask.addQuery("task", "PASTE_YOUR_SYS_ID_HERE");
incTask.addQuery("has_breached", true);
incTask.orderBy("planned_end_time"); // Sorting in Ascending order
incTask.query();
if (incTask.next()) { // Returning only first record
gs.info("This is the incident number: " + incTask.task.getDisplayValue());
gs.info("This is the breach times: " + incTask.planned_end_time.getDisplayValue()); //breach time column
}
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2023 01:05 PM
@Community Alums : If there are chances of more records, it's best to do it at the query level, like I did in the code, or else you may end up storing all the values in a list, sorting them, and finally returning the first element (the least sorted) from the list.
If this is just for testing purposes, you can use the "GlideDateTime" API and its functions like "before" and "after" methods to check which is the earliest among the two.
https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server/c_APIRef
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:43 AM - edited 12-21-2023 04:44 AM
Oh, I think I understand what you mean now. The method orderBy is giving you the earliest/soonest time by default, which is the reason why it returned only one record. Correct?
I get it now. Super helpful. See, my technical lead was saying I needed to compare values and that is why I didn't get it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 04:56 AM
That’s correct and with this approach it makes your life easy as system does the sorting and returns only the earliest date.
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 05:07 AM
Thank you so much!!!!!!!!