calculate business duration based on start date & end data via after business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 07:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 07:35 AM
Hi @Mabubi,
I tested in Scripts - Background using:
var inc = new GlideRecord('incident');
inc.addQuery('state', '7');
inc.query();
while (inc.next()) {
var start = new GlideDateTime(inc.work_start.getDisplayValue());
var end = new GlideDateTime(inc.work_end.getDisplayValue());
var duration = GlideDateTime.subtract(start, end);
inc.business_duration = duration;
//current.update();
gs.info("start: " + start + ", end: " + end + ", business_duration: " + duration);
}
/*
var start = new GlideDateTime(current.work_start.getDisplayValue());
var end = new GlideDateTime(current.work_end.getDisplayValue());
var duration = GlideDateTime.subtract(start, end);
current.business_duration = duration;
//current.update();
gs.info("business_duration: "+current.business_duration);
*/
The code at the end commented out should work in your BR.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 10:56 PM
Hello,
Here’s how you can modify your script to calculate the business duration without the need for current.update()
:
(function executeRule(current, previous /*null when async*/) {
// Log to check if the business rule is executing
gs.info("Check BR duration");
// Ensure that both start and end dates are set
if (current.work_start && current.work_end) {
var start = new GlideDateTime(current.work_start);
var end = new GlideDateTime(current.work_end);
// Subtract the start time from the end time
var duration = GlideDateTime.subtract(start, end);
// Store the calculated duration in the business_duration field
current.business_duration = duration.getDurationValue();
// Log the business duration for debugging
gs.info("business_duration: " + current.business_duration);
} else {
gs.info("Either work_start or work_end is not set.");
}
// Avoid using current.update() here
})(current, previous);
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 01:08 PM
I defined a business rule, using your code. I did this for the incident table, I don't know what table your business rule is defined on. See following screen shots:
The script from the Advanced tab:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var start = new GlideDateTime(current.work_start.getDisplayValue());
var end = new GlideDateTime(current.work_end.getDisplayValue());
var duration = GlideDateTime.subtract(start, end);
current.business_duration = duration;
//current.update();
gs.addInfoMessage("start: " + start + ", end: " + end + ", business_duration: " + current.business_duration);
})(current, previous);
Test result, before:
Using view that shows the three fields we're dealing with here (upper right). Next screen shot shows the BR results after and update:
If you're not getting the same results, then more is at play here. Provide sufficient details for Cummunity members to help.