Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Exclude weekends from the calculation of date time on request item form in business rule

Swapnil Bhakar4
Kilo Explorer

Hi Team,

I have a requirement that i have to exclude the weekends from the calculation of estimated time delivery field. On requested item we have one field called estimated delivery which is a datetime field. I am setting this field value on the basis of the field delivery time which is present on catalog form. 

Now i am calculating the estimated delivery field when the item is requested and in that adding the delivery time value which defined on catalog form.

Estimated Delivery = created + delivery time

I am able to calculate the estimated delivery field value but i am not able to exclude the weekends i.e. saturday and sunday in script.

Can anyone please help me that how can i exclude the weekends and adjust in my below business rule:

Business Rule: After Insert

Table: Requested Item

Script:

var daysStr = current.cat_item.delivery_time.dateNumericValue(); // Delivery time field value present on catalog item form

var created = current.opened_at.getDisplayValue(); // Request item created date and time

var gdt = new GlideDateTime(created);

var ed = new GlideDateTime(gdt);

ed.add(daysStr);

current.estimated_delivery = ed.getDisplayValue();

current.update();

 

Thanks,

Swapnil

4 REPLIES 4

johnfeist
Mega Sage

Hi Swapnil,

Here is what I'm using to do something similar on the incident table.  The code is contained in a BR and does require that there be an existing schedule to work against.  Please let me know if you need any clarifications or additional information.

:{)

gs.include('DurationCalculator');
//Calculate three business days forward and store it in on_hold_start on hold start is a field I added to the table			

var usrSchedule = "090eecae0a0a0b260077e1dfa71da828"; // This is the sys_id of the schedule you want to use.
var days = 3;   //you can substitute your delivery days here
var hours = getBusHours(usrSchedule);
var defdur = "";
defdur = ((hours*3600) * days);
if (defdur != ""){
        var dur = new DurationCalculator();
	dur.setSchedule(usrSchedule);
	dur.setStartDateTime('current.sys_updated_on');  //use whatever start date/time is appropriate
	dur.calcDuration(defdur);
	current.u_on_hold_start = dur.getEndDateTime();  //replace u_on_hold_start with estimated_delivery 
}
			


// Retrieve Business hours on Schedule
function getBusHours(newschedule){
	var diffSeconds = 86400;
	var gr = new GlideRecord('cmn_schedule_span');
	gr.addEncodedQuery('schedule=' + newschedule + '^type=NULL');
	gr.query();
	if(gr.next()){
		diffSeconds = gs.dateDiff(gr.start_date_time.getDisplayValue(), gr.end_date_time.getDisplayValue(), true);
	}
	var hours = (diffSeconds/60)/60;
	return hours;
}
Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hi John,

 

Thanks for your response!

 

I gone through your code but i am not sure whether it will work or not for my requirement. I just want to exclude the saturday and sundays not the business hours.

So could you please help me to that how can i adjust this in my current script?

 

Thanks,

Swapnil Bhakare

Hi swapnil,

I'm using the code as is and it does exactly what I need which sounds like what you need (e.g. count forward some number of business days).  The schedule I'm using is the OOTB 8-5 Weekdays excluding holidays.

:{)

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Swapnil Bhakar4
Kilo Explorer

Hi,

Does anybody have any ideas on this?

Thanks,

Swapnil Bhakare