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.

get datetime based on Schedule through script

Kumar38
Kilo Sage

Hi,

I need to populate due date field dynamically based on days selected.

This schedule would be 8 - 5 excluding business days . Tried this and end times aren't in between 8 to 5.

start date isnt in my time zone.

var slaDays = 3;
var startDate = new GlideDateTime();
gs.info('Log--startDate---' + startDate);
var days = parseInt(slaDays);
//assuming there are 9 business hours
days = days*9;
var dur = new GlideDuration(60 * 60 * 1000 * days);
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); //8-5 weekdays excluding holidays
var end = schedule.add(startDate, dur);
gs.info('Log--end---' + end);

 

*** Script: Log--startDate---2022-01-26 12:22:04
*** Script: Log--end---2022-01-31 16:00:00
2 REPLIES 2

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

You can use the answer from this thread:-

https://community.servicenow.com/community?id=community_question&sys_id=c3b90bb01bd909101e579979b04bcbda&anchor=answer_abb1937c1b9d09101e579979b04bcb1e

Please mark answer correct/helpful based on Impact

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Try this

var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('sys_id', '090eecae0a0a0b260077e1dfa71da828'); //ideally you shoukd search by name and not hardcode sys_id
if (typeof GlideSchedule != 'undefined')
   var sched = new GlideSchedule(schedRec.sys_id);
else
   var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);

//Get the current date/time in correct format for duration calculation
var currentDateTime = new GlideDateTime();
currentDateTime.setDisplayValue(gs.nowDateTime());

//Set the amount of time to add (in seconds)
var timeToAdd = 97200; //this is 9*3*60*60 
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, '');

 

-Anurag

-Anurag