Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business rules to check current time is before 8:30am

Jake36
Kilo Contributor

Hi guys, 

I'm currently writing a script to calculate delivery date for CR based on man days entered by users as the following:

Man dayDelivery Days
1 - 5 30
6 - 1045
10 - 3060
30 above90

However I notice that there's a behavior that if the CR is approved before 8:30am, it will calculate current day as 1 day whereas it is suppose to start counting the next day as first day. Can anyone help with the scripting to check if the time is before 8:30am it will add 31 days instead of 30?

Thanks in advance!

Jake

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

	// Add your code here
var update = current.sys_updated_on.getDisplayValue(); 
var gd = new GlideDate();  
update  = gd.getByFormat('yyyy-MM-dd'); //Format
	
var gDateTime = new GlideDateTime(current.sys_updated_on.getDisplayValue());
var gTime = gDateTime.getTime();
var time = gTime.getByFormat('HH:mm:ss');

//var getDateTime = new GlideDateTime(current.sys_updated_on);
//var gDate = getDateTime.getDate();
//var date = gDate.getByFormat('yyyy-MM-dd');

var DateTime = update + ' ' + time;

var days = current.u_estimated_total_mandays;
var plus = '';
	// days less than 5 or equal 5
if(5 >= days){ 
  plus = 30; // add 30 Calendar days
	}
	
// days greater than 5 and days less than 10 or equal to 10
if(5 < days && days <= 10){ 
  plus = 45; // add 45 Calendar days
}
// days greater than 10 and days less than 30 or equal to 30
if(10 < days && days <= 30){ 
  plus = 60; // add 60 Calendar days
}
// days greater than 30
if(days > 30){ 
  plus = 90; // add 90 Calendar days	
}

var gdtDelivery = new GlideDateTime(DateTime);
gdtDelivery.addDays(plus);
current.u_delivery_date = gdtDelivery.getValue();
})(current, previous);

 

1 ACCEPTED SOLUTION

Manish Vinayak1
Tera Guru

Hi Jake,

The issue with calculation before 8:30 AM is due to timezone difference. When you use GlideDateTime() to get current date it returns the date time value in GMT. It could be giving you last day's date before 8:30 AM. I assume you want to add the days to the current / that day's date to determine the delivery date. I don't understand the initial lines of code which are getting date and time separately to form a date.

If my assumption is correct, and you want to add days to current date based on the number of mandays selected, you could try the following script:

var days = current.u_estimated_total_mandays;
var plus = '';
	// days less than 5 or equal 5
if(5 >= days){ 
  plus = 30; // add 30 Calendar days
	}
	
// days greater than 5 and days less than 10 or equal to 10
if(5 < days && days <= 10){ 
  plus = 45; // add 45 Calendar days
}
// days greater than 10 and days less than 30 or equal to 30
if(10 < days && days <= 30){ 
  plus = 60; // add 60 Calendar days
}
// days greater than 30
if(days > 30){ 
  plus = 90; // add 90 Calendar days	
}

//This will get the date based on GMT Timezone
var gdtDelivery = new GlideDateTime();
gdtDelivery.addDays(plus);
//getLocalDate will get the date converted to local timezone
current.u_delivery_date = gdtDelivery.getLocalDate();

Give it a try and see how it goes.

Thanks,

Manish

View solution in original post

3 REPLIES 3

vandna
Tera Guru

Hi,

Here is the script.

 

var currentDateTime = new GlideDateTime();

 

var nowTime = currentDateTime.getTime();

var nowTimeVal = notTime.getNumericValue();

 

var gTime = new GlideTime();

gTime.setValue('08:30:00');

var gTimeVal = gTime.getNumericValue();

if (nowTimeVal < gTimeVal)

  gs.print("Before 8:30");

else

 gs.print("after 8:30");

Manish Vinayak1
Tera Guru

Hi Jake,

The issue with calculation before 8:30 AM is due to timezone difference. When you use GlideDateTime() to get current date it returns the date time value in GMT. It could be giving you last day's date before 8:30 AM. I assume you want to add the days to the current / that day's date to determine the delivery date. I don't understand the initial lines of code which are getting date and time separately to form a date.

If my assumption is correct, and you want to add days to current date based on the number of mandays selected, you could try the following script:

var days = current.u_estimated_total_mandays;
var plus = '';
	// days less than 5 or equal 5
if(5 >= days){ 
  plus = 30; // add 30 Calendar days
	}
	
// days greater than 5 and days less than 10 or equal to 10
if(5 < days && days <= 10){ 
  plus = 45; // add 45 Calendar days
}
// days greater than 10 and days less than 30 or equal to 30
if(10 < days && days <= 30){ 
  plus = 60; // add 60 Calendar days
}
// days greater than 30
if(days > 30){ 
  plus = 90; // add 90 Calendar days	
}

//This will get the date based on GMT Timezone
var gdtDelivery = new GlideDateTime();
gdtDelivery.addDays(plus);
//getLocalDate will get the date converted to local timezone
current.u_delivery_date = gdtDelivery.getLocalDate();

Give it a try and see how it goes.

Thanks,

Manish

Hi Manish,

 

Thanks a lot! The script is working great.

However one more thing is that the current.u_delivery_date is stamped to 8am fixed.

Is there any way I could have the calculation goes 30 days down to minute or seconds? 

For example:

Man days is entered at the time 01/01/19 15:30:00, the delivery date would be 30/01/19 15:30:00.