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

GP6
Giga Contributor

Well done @Mike Patel .  This gave me the pieces of the puzzle I needed in order to solve the issue.  This works very well.  Thank you.

gana2
Tera Contributor

Hii Mike,

I need to subtract the like I need to get the before 15 business days date (exclude weekends).

Thanks

 

ronaldanand
Tera Contributor

Thank alot ..................

Wonderful... You are made my day .... Thanks for kind help ...

Many many Thanks 🙂

Glenn Vattudal
Tera Expert

Hi.

I've used this post to make my own script and wanted to share the info.

1. I use a schedule in my own instance

2. I only want to set a start date

3. I use the schedule that takes into account that each working day is 11 hours so I add 4 days ahead and if not an error msg is produced.

4. This is used in Service Portal for a widget to order a Service Technician.

Script include 'QuarantineValidate':

var QuarantineValidate = Class.create();
QuarantineValidate.prototype = Object.extendsObject(AbstractAjaxProcessor,
{
	ValidateDate: function() {
		
		var days = this.getParameter('sysparm_needed_by', newDateTime);
		var cal = new GlideSchedule('75146082db625b007210fba668961971');// sys_id of the Schedule used
		var currentDateTime = new GlideDateTime();
		var dur = new GlideDuration(60*60*11*1000*4); //4 days. It calculates from the time of the ordering and 4 days ahead. 
		var newDateTime = cal.add(currentDateTime, dur,'');
		return newDateTime.getDisplayValue();
	} ,
	
	type: 'QuarantineValidate'
});

Client Script: 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	// **************************************************************
	// * Schedule is defined in Script Include 'QuarantineValidate' *
	// **************************************************************
	g_form.hideFieldMsg('start_date');
	var val = g_form.getValue('start_date');
	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");
		if(answer>val){
			g_form.showFieldMsg('start_date', "Date has to be 5 working days in the future" , 'error');
			g_form.clearValue('start_date');
			g_form.setMandatory('start_date');
		} 
	}
}

@Glenn Vattudal thank you so much for this.  Your example here got me over the line.  Well done.