Date Validation

brown9394
Tera Expert

Hi guys, I have a requirement to validate "complete" date on the form.

I have 2 fields - "Open" Date & "Complete" Date. How can I do validation by using OnChange to make sure "Complete" date is always after the "Open" date? Users should not be able to put in a "Complete" date prior to "Open" Date. I'm new to scripting, and have no clue how to go about coding this. Any help would be appreciated.

1 ACCEPTED SOLUTION
5 REPLIES 5

Chuck Tomasi
Tera Patron

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi James,



You may also find below blog helpful


Comparing Client Dates


carlh
Kilo Guru

James, this is the script on the business rule we have on our Contracts Table to check the Start and End dates of the contract term.  



Warning: I am new to scripting so any questions you have would need help from a more experienced community member but it's working so I thought I would try to help...Hope the example helps



When to run: Before
Insert = True


Update = True



* Our condition - current.starts.changes() || current.ends.changes() || current.renewal_date.changes() || current.renewal_end_date.changes()



//check the contract dates
if ((current.starts.nil()) && (!current.ends.nil())) {
      gs.addErrorMessage('Start date must be specified');
      current.setAbortAction(true);
} else if ((!current.starts.nil()) && (!current.ends.nil())) {
      var starts = current.starts.getGlideObject().getNumericValue();
      var ends = current.ends.getGlideObject().getNumericValue();
      if (starts > ends) {
              gs.addErrorMessage('Start date must be before end date');
              current.setAbortAction(true);
      }
}


//check the renewal/extension dates
if ((current.renewal_date.nil()) && (!current.renewal_end_date.nil())) {
      gs.addErrorMessage('Renewal start date must be specified');
      current.setAbortAction(true);
} else if ((!current.renewal_date.nil()) && (!current.renewal_end_date.nil())) {
      var starts = current.renewal_date.getGlideObject().getNumericValue();
      var ends = current.renewal_end_date.getGlideObject().getNumericValue();
var rightNow = new GlideDateTime().getNumericValue();

      if (starts > ends) {
              gs.addErrorMessage('Renewal start date must be before end date');
              current.setAbortAction(true);
      }

if(ends < rightNow) {
  gs.addErrorMessage('Renewal end date must be after current date');
              current.setAbortAction(true);
}
}


Thanks Carl, I ended up using the simpler version that Pradeep & Chuck posted which just checked against the open date onChange.