Compare Dates in Business Rule - Format Issue

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 10:35 AM
I'm trying to prevent users from adding time to time cards in the future. My problem, I think, is that the dates I'm comparing are in different formats. I'm also not sure if I can just say if date1 is > date 2 and get the correct response....
I need to compare the current date (curDate) in format MM-DD-YYYY to a date (dateEntered) in the format of YYYY-MM-DD. I've read numerous posts and wiki articles, but am not finding what I'm looking for.
Here's my script:
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
gs.log('We are doing date validation on this time card submission.');
var curDate = gs.now(); //what is the date now
gs.log('curDate = ' + curDate);
var dayChanged = '';
var weekStart = current.week_starts_on;
gs.log('weekStart = ' + weekStart);
var ourDate = new GlideDateTime(curDate);
var dateEntered = new GlideDateTime();
var curCat = current.category;
gs.log('curCat = ' + curCat);
if (current.isNewRecord()) //if this is a new record, figure out which day changed
{
gs.log('This is a new time card.');
if (current.monday > 0) {
dayChanged = 'mon';
}
else if (current.tuesday > 0) {
dayChanged = 'tue';
}
else if (current.wednesday > 0) {
dayChanged = 'wed';
}
else if (current.thursday > 0) {
dayChanged = 'thu';
}
else if (current.friday > 0) {
dayChanged = 'fri';
}
else if (current.saturday > 0) {
dayChanged = 'sat';
}
else if (current.sunday > 0) {
dayChanged = 'sun';
}
gs.log('dayChanged on new record is ' + dayChanged);
}
else
{
gs.log('This is an existing time card.'); //get the latest day that changed
if (current.monday != previous.monday)
{
dayChanged = 'mon';
gs.log('Mon changed');
}
if (current.tuesday != previous.tuesday)
{
dayChanged = 'tue';
gs.log('Tue changed');
}
if (current.wednesday != previous.wednesday)
{
dayChanged = 'wed';
gs.log('Wed changed');
}
if (current.thursday != previous.thursday)
{
dayChanged = 'thu';
gs.log('Thu changed');
}
if (current.friday != previous.friday)
{
dayChanged = 'fri';
gs.log('Fri changed');
}
if (current.saturday != previous.saturday)
{
dayChanged = 'sat';
gs.log('Sat changed');
}
if (current.sunday != previous.sunday)
{
dayChanged = 'sun';
gs.log('Sun changed');
}
gs.log('dayChanged on existing record is = ' + dayChanged);
}
//now get the date where the latest time was entered
if (dayChanged == 'mon')
{
dateEntered = weekStart;
}
else if (dayChanged == 'tue')
{
ourDate.addDaysLocalTime(1);
dateEntered = ourDate.getDate();
}
else if (dayChanged == 'wed')
{
ourDate.addDaysLocalTime(2);
dateEntered = ourDate.getDate();
}
else if (dayChanged == 'thu')
{
ourDate.addDaysLocalTime(3);
dateEntered = ourDate.getDate();
}
else if (dayChanged == 'fri')
{
ourDate.addDaysLocalTime(4);
dateEntered = ourDate.getDate();
}
else if (dayChanged == 'sat')
{
ourDate.addDaysLocalTime(5);
dateEntered = ourDate.getDate();
}
else if (dayChanged == 'sun')
{
ourDate.addDaysLocalTime(6);
dateEntered = ourDate.getDate();
}
gs.log('dateEntered = ' + dateEntered);
//compare the dates and error if time being entered is in the future
if (dateEntered > curDate)
{
gs.log('The date entered is in the future.');
gs.addErrorMessage('You cannot submit time for dates in the future. Your time card was not created or updated.');
current.setAbortAction(true);
}
}
TIA (again!)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2016 12:39 PM
I think you can save some effort by reviewing what is available for use in the GlideDateTime - ServiceNow Wiki API. Here i would specifically look at the CompareTo() and see if you can leverage that.