calculate business duration based on start date & end data via after business rule

Mabubi
Tera Contributor
calculate business duration based on start & end date fields.Start date value will set once state changes to work in progress & end date will set once state move to close complete. This scenario which Business rule will use.
i have used after business rule. But  can't use current.update() right.so i have writtern aync also its not setting the business duration.
not getting this log as well -gs.info("business_duration"+current.business_duration);
 
Script:
(function executeRule(current, previous /*null when async*/) {

 

    // Add your code here
    gs.info("check br 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);

 

})(current, previous);
 
 
could you please confirm me any idea.how to proceed any other option.
7 REPLIES 7

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.

Ravi Gaurav
Giga Sage
Giga Sage

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/

Bert_c1
Kilo Patron

@Mabubi,

 

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:

Screenshot 2024-08-23 155950.png 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:

Screenshot 2024-08-23 160037.png

Using view that shows the three fields we're dealing with here (upper right).  Next screen shot shows the BR results after and update:

Screenshot 2024-08-23 160120.png

If you're not getting the same results, then more is at play here. Provide sufficient details for Cummunity members to help.