To compare two dates and set a business rule based on which is earlier

ahammoud
Tera Guru

I have an item created in the Service Catalog called "Order Business Cards".
I set up a bunch of variables in it, one of them is called "Delivery Date" (delivery_date) that is specified by the user.
I want to compare the 'delivery_date' specified by the user with the actual current date and then alert them if the 'delivery_date' is within 7 days from current date (less than a week from today). I set up a Business Rule on the Requested Item table (sc_req_item) set to 'before' insert.
What I did, is created 2 variables one equal to the value set by the user and the other to current date + 7 days. I verified that the variables are containing the correct value, but I am trying to use the compare Dates methods, to generate the message, but it doesn't work:


var date = current.variables.delivery_date;
var date2 = new GlideDate(); //set date2 = today's date
date2.addDays(+7); // adds 7 days to date2

gs.addInfoMessage(date); //displays on page the correct date
gs.addInfoMessage(date2); //displays on page the correct date

var test = gs.print(date.compareTo(date2));
if ( test == '-1'){
gs.addInfoMessage('Please allow 7 days for delivery');
}



Note: this is where compare dates method is found:
https://wiki.servicenow.com/index.php?title=GlideDateTime#compareTo.28o.29

8 REPLIES 8

ahammoud
Tera Guru

but how would I make the business rule stop the user from proceeding and submitting the request and return them (or keep them) on the same screen ? I want the message to display and stop the user from submitting the request until they CORRECT the date.


In you If statement add an abortAction.
http://wiki.servicenow.com/index.php?title=Business_Rules#Aborting_a_Database_Action_in_a_Business_Rule


johnram
ServiceNow Employee
ServiceNow Employee

The ServiceNow Wiki content is no longer supported. Updated information about this topic is located here: Business Rules




Visit http://docs.servicenow.com for the latest product documentation


You can do that by using:

current.setAbortAction(true);

inside the if condition.