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

ronaldanand
Tera Contributor

This is my script ..plz correct me

 

Client script on change

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',newDateTime);
   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
        }

   
}

 

Script include

var QuarantineValidate = Class.create();
QuarantineValidate.prototype = Object.extendsObject(AbstractAjaxProcessor,
    {
    ValidateDate: function(days) {
    //table:global
var cal = GlideCalendar.getCalendar('b13be85ddbbe47408567fd7aae9619f1');
var duration = new GlideDuration();
gs.print(nowPlusNCalDays(6));
        //duration.setValue(1000*60*60*8*days)
duration.setValue(1000*60*60*8*6); //this will add 1000ms*60sec*60mins*8hour(biz hours in a day)*(days parameter)
var actualDateTime = new GlideDateTime();
gs.print(actualDateTime);
var newDateTime = new GlideDateTime();
actualateTime.setDisplayValue(gs.nowDateTime());
newDateTime.setValue(cal.add(actualDateTime,duration));
return newDateTime.getDisplayValue();
} ,
    
    type: 'QuarantineValidate'
});

your client needs date to pass in not newDateTime

ga.addParam('sysparm_needed_by', newDateTime);

Sorry , I am not getting .. please explain ...

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
	}
	
	
}

Last request ...

Please help me to display only day value like 04-06-2019 format..