How to calculate 5 working days using scehdule and display the future date

ronaldanand
Tera Contributor

Hi All ,

 I have to calculate 5 working days using US holidays calendar schedule based and i try to use below script.

Kindly check and help me how to use this on change client script and script include in the same script.

I have tried my level its  not working pls help me to fix this issue

BACKGROUND SCRIPT

var cal = GlideCalendar.getCalendar('b13be85ddbbe47408567fd7aae9619f1');

//table:global

gs.print(nowPlusNCalDays(6));

function nowPlusNCalDays(days) {

//set the calendar sysid to your calendar

//var cal = Packages.com.glide.schedule.GlideCalendar.getCalendar('b13be85ddbbe47408567fd7aae9619f1');

var cal = GlideCalendar.getCalendar('bea4c03cc611227801d411166792a145');

var duration = new GlideDuration();

//specify the number of milliseconds to add, based on calenadar

//this will add 1000ms*60sec*60mins*8hour(biz hours in a day)*(days parameter)

//the calculation makes more sense if you think of it as how many business hours to add, not days.

duration.setValue(1000*60*60*8*days);

var actualDateTime = new GlideDateTime();

gs.print(actualDateTime);

var newDateTime = new GlideDateTime();

actualDateTime.setDisplayValue(gs.nowDateTime());

newDateTime.setValue(cal.add(actualDateTime,duration));

return newDateTime.getDisplayValue();

}

 

IF I select 5BD it will calculate 5 working days and if i select 15BD it will get the value 15 based on field change.

Please check and help me

 

Thanks

 

 

1 ACCEPTED SOLUTION

change your script includes to below. make sure client callable is checked.

var QuarantineValidate = Class.create();
QuarantineValidate.prototype = Object.extendsObject(AbstractAjaxProcessor,
{
	ValidateDate: function() {
		
		var days = this.getParameter('sysparm_needed_by');
		var cal = new GlideSchedule('b13be85ddbbe47408567fd7aae9619f1');
		
		var currentDateTime = new GlideDateTime();
		currentDateTime.setDisplayValue(gs.nowDateTime());
		
		var dur = new GlideDuration();
		dur.setValue(60*60*24*1000*days); 
		
		var newDateTime = cal.add(currentDateTime, dur, '');
		
		return newDateTime.getDisplayValue();
	} ,
	
	type: 'QuarantineValidate'
});

change client script to below

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '')
		{
		return;
	}
	
	var val=g_form.getValue('u_euc_ci_status');
	
	if (val == 51){
		var ga = new GlideAjax('QuarantineValidate');
		ga.addParam('sysparm_name','ValidateDate');
		ga.addParam('sysparm_needed_by', val);
		ga.getXML(callBack);
	}
	function callBack(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
		g_form.setValue('u_quarantine_end_date', answer); // put in the variable name you want to show or hide according needed by date
	}
	
	
}

View solution in original post

19 REPLIES 19

Mike Patel
Tera Sage

Try using 

var sched = new GlideSchedule('55ebbbf15f002000b12e3572f2b47714');
var currentDateTime = new GlideDateTime();
currentDateTime.setDisplayValue(gs.nowDateTime());
var dur = new GlideDuration(60 * 60 * 24 * 1000 * 5); //5 days
var newDateTime = sched.add(currentDateTime, dur, '');

gs.print(newDateTime.getDisplayValue());

I got following reply ...

Adding 5 Days to 2019-06-04 16:42:20 using schedule null ran too long without hitting schedule times - using simple date addition
*** Script: 2019-06-09 16:42:20

 

Its not calculating exclude holidays , Its simply adding 5 days to current date

Did you change the sysid of calendar ?

Thanks Mike...

 

Please help me

How to get RITM & Catalog Item details from sysapproval_approver user Based details.

Pls check the below URL and help me fix the issue

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