Set RITM Due Date to date/Time variable plus 3 days 4 hours

booher04
Tera Guru

I have another item where I have set the due date to 10 business days from the opened date of the item, however I have a little more tricky one.  I need to take the date/time from a variable(date_time_deployment) the user selects and add 3 business days and 4 hours to it for the due date.  I put together this script to get the 3 days as a start but it's not working correctly.  Any ideas on how to get this accomplished?  I'm using a business rule to accomplish this.

 

var schedRec = new GlideRecord('cmn_schedule');

schedRec.get('name', '8-8 weekdays excluding holidays');

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 = current.variables.date_time_deployment;

//Set the amount of time to add (in seconds)

var timeToAdd = 3 * 43200;

durToAdd = new GlideDuration(timeToAdd*1000);

var newDateTime = sched.add(currentDateTime, durToAdd, '');

//Set the 'due_date' field to the new date/time

current.due_date = newDateTime;

1 ACCEPTED SOLUTION

If its a business rule on Requested Item [sc_req_item] table then 

current.variables.date_time_deployment

should give you the value.

 

I tested and this code should work for you.

var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '8-8 weekdays excluding holidays');

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(current.variables.date_time_deployment);

//Set the amount of time to add (in seconds)
var timeToAdd = 3 * 43200 + 4 * 3600;
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, '');

//Set the 'due_date' field to the new date/time
current.due_date = newDateTime;

 

In case you are writing this BR in after or async then dont miss to add this line at the end of above script.

current.update();

 

-Tanaji

Please mark response correct/helpful if applicable

View solution in original post

12 REPLIES 12

Could you try following code-

var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '8-8 weekdays excluding holidays');

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(current.date_time_deployment.getDisplayValue());

//Set the amount of time to add (in seconds)
var timeToAdd = 3 * 43200 + 4 * 3600;
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, '');

//Set the 'due_date' field to the new date/time
current.due_date = newDateTime;

 

-Tanaji
Please mark reply correct/helpful if applicable

It's a business rule.  I'm thinking the issue is that it's not finding the variable because I'm not having it pull from the RITM variable pool.

If its a business rule on Requested Item [sc_req_item] table then 

current.variables.date_time_deployment

should give you the value.

 

I tested and this code should work for you.

var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '8-8 weekdays excluding holidays');

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(current.variables.date_time_deployment);

//Set the amount of time to add (in seconds)
var timeToAdd = 3 * 43200 + 4 * 3600;
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, '');

//Set the 'due_date' field to the new date/time
current.due_date = newDateTime;

 

In case you are writing this BR in after or async then dont miss to add this line at the end of above script.

current.update();

 

-Tanaji

Please mark response correct/helpful if applicable

Thank you!  That script worked great. 

One more question... we had a requirement change. 

If the date_time_deployment is set to less than 3 days from current date and time, add 3 days and 4 hours to the due_date.  If date_time_deployment is set to more than 3 days from current date and time, add 4 hours to the due_date.