We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Calculate Business Duration Excluding Weekends.

Meenal Gharat
Tera Guru

Hi  All,

How can we exclude Weekends from Business duration Calculation?

I'm getting 0 seconds as calculated Business Duration.

Can someone please help me to correct the below code.

Below is my code for reference.

function setClosureFields() {

current.active = false;
if (current.closed_by.nil())
current.closed_by = gs.getUserID();
if (current.closed_at.nil())
current.closed_at = gs.nowDateTime();

var dataChange = current.opened_at.changes() || (current.closed_at.changes() && !current.isValidField("u_resolved"));
var opened = current.opened_at.getDisplayValue();
var closed = current.closed_at.getDisplayValue();
var resolved = current.u_resolved.getDisplayValue();

var startDate = new GlideDateTime('opened');
var endDate = new GlideDateTime('closed');
var sched = new GlideSchedule();
sched.load('9fdacfe46f24ee00a3f0884f8e3ee4e6'); //sys_id of the schedule used i.e. Mon - Fri
var duration = sched.duration(startDate,endDate); current.business_duration = duration.getDurationValue();

if (dataChange || current.calendar_duration.nil())
current.calendar_duration = gs.dateDiff(opened, resolved, false);

if (dataChange || current.u_resolved.nil())
current.u_resolved = current.closed_at;

}

 

Thanks and Regards,

Meenal

 

1 ACCEPTED SOLUTION

JerryJ071847183
Tera Sage
4 REPLIES 4

JerryJ071847183
Tera Sage

Chk if this helps

 

https://community.servicenow.com/community?id=community_question&sys_id=d8e19baddbdcdbc01dcaf3231f96...

 

Mark my ANSWER as CORRECT and HELPFUL if it helped.

Hi,

 

Tried using the above link.

I got an error because of newGlideObject()

 

 

Thanks and regards,

Meenal

Cannot access this URL.

Could you help on calculating the working days ?

Meenal Gharat
Tera Guru

Hi ,

Thanks for your Help.

It worked.

 

Thanks and regards,

Meenal