Business Rule to calculate due date 3 days in the future and following the 8-5 weekdays schedule

daniellethomson
Tera Expert

I can't seem to figure out how to add a schedule to a script that calculates a custom targeted due date field based on a requested_date variable. I would like the script to take todays date and add 3 days excluding weekends. Does anyone have this script already?

Thanks!

1 ACCEPTED SOLUTION

JohnG3
Mega Guru

Hi Danielle,



Strange, I created a test Catalog Item with a Date/Time variable and used the script, but changed line 22 to just:


dueDateGdt;



It seemed to work for me to set the date for 3 days.



Screenshot of Date/Time variable:


test_date.png


Screenshot of Default Value of Date/Time variable:


test_date_default_value.png



Default Value code:



javascript: var nowGdt = new GlideDateTime(); var nowGdt = new GlideDateTime(); var myScheduleName = '8-5 weekdays'; var dueDays = 3; var dueWorkingHours = 8; var dueSeconds = dueDays*dueWorkingHours*60*60; var leadTime = new GlideDuration(dueSeconds*1000); var dueDateGdt; var schedRec = new GlideRecord('cmn_schedule'); if (schedRec.get('name', myScheduleName)) {var sched = new GlideSchedule(schedRec.sys_id); dueDateGdt = sched.add(nowGdt, leadTime, '');} dueDateGdt;



Thanks,



John



P.S.   Normally, we would create a custom Script Include with a helper function:   getDueDate('schedule_name', 'number_of_days') and then use that for the variable default value:   javascript:new myCatalogVariableHelper().getDueDate('8-5 Weekday', 3);


View solution in original post

15 REPLIES 15

Chuck Tomasi
Tera Patron

Hi Danielle,



This is a fairly common question and one that is documented. Take a look here.



http://wiki.servicenow.com/index.php?title=Using_DurationCalculator_To_Calculate_a_Due_Date#gsc.tab=...


thanks Chuck. I have been trying to create my script from that but It's only pulling the getnow date and time into the variable. I'm a little new to scripting so I must be missing something.



// Get the datetime now


var nowGdt = new GlideDateTime();



// Get the datetime now


var nowGdt = new GlideDateTime();


// The name of the schedule  


var myScheduleName = '8-5 weekdays';  


 


// The basis of our calculation  


var dueDays = 3;  


var dueWorkingHours = 8;  


 


var dueSeconds = dueDays*dueWorkingHours*60*60;  


var leadTime = new GlideDuration(leadTimeSeconds*1000);  



var dueDateGdt;  


var schedRec = new GlideRecord('cmn_schedule');  


if (schedRec.get('8-5 weekdays', myScheduleName)) {                


        var sched = new GlideSchedule(schedRec.sys_id);  


        dueDateGdt = sched.add(nowGdt, leadTime, '');  


}  


dueDateGdt;


Can you tell me where this script is being used? Is it a business rule? A workflow script?


Hi Chuck,



I am using this script in the default value of the variable.