- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2025 12:42 AM
Hello all,
On the Change request, whenever the Actual End is populated by the system, then the difference between Actual Start and Actual End has to be auto populated in the Duration field (Which contains Days and Hours).
Note: Duration field backend name: (u_duration)
Can someone tell me how can I do this?
Regards,
Lucky
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2025 09:54 PM
it worked for me with the 1st script I shared
(function executeRule(current, previous) { // Add your code here
var startDate = new GlideDateTime(current.start_date);
var endDate = new GlideDateTime(current.end_date);
current.u_duration = GlideDateTime.subtract(startDate, endDate);
})(current, previous);
BR
Output: I gave 2 hours difference, it populated 2 hours, I gave 2mins difference, it populated 2mins
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2025 12:56 AM
Hi @Lucky1
Try "Before Business rule" as belwo to calculate the duration.
(function executeRule(current, previous /*null when async*/ ) {
var gdtStart = new GlideDateTime(current.getValue('work_start'));
var gdtEnd = new GlideDateTime(current.getValue('work_end'));
var duration = GlideDateTime.subtract(gdtStart, gdtEnd); //the difference between gdt1 and gdt2
current.u_duration = duration.getDurationValue();
})(current, previous);
Output:
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2025 12:59 AM
you can use before update business rule to populate the custom duration field
(function executeRule(current, previous) { // Add your code here
var startDate = new GlideDateTime(current.start_date);
var endDate = new GlideDateTime(current.end_date);
current.u_duration = GlideDateTime.subtract(startDate, endDate);
})(current, previous);
OR
(function executeRule(current, previous) { // Add your code here
var startDate = new GlideDateTime(current.start_date);
var endDate = new GlideDateTime(current.end_date);
var duration = endDate.getNumericValue() - startDate.getNumericValue();
current.u_duration = new GlideDuration(duration);
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2025 01:54 AM
Thank you Siva and Ankur,
Wil check and get back.
Regards,
Lucky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2025 09:32 PM - edited 05-08-2025 09:39 PM
Hello Ankur and Siva,
Good day!!
I have tried both of your logics but it's not updating with correct value.
From the below pic, it has to be updated with 2 mins only.
I put the log message for duration, and it is showing
Can you please guide me?
Regards,
Lucky