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

Last request ...

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

 

Right before return in script includes add var date = newDateTime.getDisplayValue().toString().split(‘ ‘); return date[0];

and remove other return

Hello Mike

Good Day!!!

 Again i have one more issue using this script, When ever I update the Status 5bd or 15bd it calculate date as expected based on calendar working days.

The problem is it only working on Client side - form update only. If i update via list view its not calculating business days value.

In same condition I used in Business rule its calculating date , But the problem is when ever am doing mass update it will calculate the date if I do not made any change  tn he  STATUS 5BD or 15 BD also, It  calculate the date  from mass update dated to next 5 business days and 15 business days based on previous selection.

It should not update if i am do not made any changes status (5bd or 15bd),only it will do once i change the status , it should allow to update other field updates.

Please help me to fix this issue.

I attached the reference screnshot and code below. Kindly check and help me to fix this issue

(function executeRule(current, previous /*null when async*/) {

    var eucCIStatus = current.u_euc_ci_status;
    var dd = 0;
    if(eucCIStatus == '51')
        {
            dd = 6;
        }
    
    if(eucCIStatus == '52')
        {
            dd = 16;
        }
    var cal = new GlideSchedule('b13be85ddbbe47408567fd7aae9619f1');
        
       var days = dd;
        var currentDateTime = new GlideDateTime();
        currentDateTime.setDisplayValue(gs.nowDate());
        
        var dur = new GlideDuration();
        dur.setValue(60*60*24*1000*days);
        var newDateTime = cal.add(currentDateTime, dur, '');
        var date = newDateTime.getDisplayValue().toString().split(' ');
        var fDate = date[0];
      current.u_quarantine_end_date = fDate;
       

find_real_file.png

 

Thanks in Advance

 

Mike

i applied same in BR but its nor working in mass update, can you plz hel me to fix this issue.

 

i update the issue in below link , kindly have a look and help me.

https://community.servicenow.com/community?id=community_question&sys_id=1c45cb98dbf63f041cd8a345ca96193e

Thanks.

Mike

i applied same in BR but its nor working in mass update, can you plz hel me to fix this issue.

 

i update the issue in below link , kindly have a look and help me.

https://community.servicenow.com/community?id=community_question&sys_id=1c45cb98dbf63f041cd8a345ca96193e

Thanks.