How to restrict a date & time field using business rule

symonflores_23
Tera Guru

In Change Management, we want to restrict a date & time field named 'start_date' so that users can only select dates after a specific date and time.

For example:

- If a user submits a change request on Thursday or Friday, the 'start_date' variable should only allow selection from next Tuesday at 5:00 PM onwards. All dates from the current date (Thursday/Friday) up to Tuesday at 5:00 PM should be restricted. However, if a user selects a Monday or Tuesday in the following weeks or a month from the current date, they should be able to proceed.

- If a user submits a change request on Monday or Tuesday, the 'start_date' variable should only allow selection from Thursday at 5:00 PM onwards. All dates from the current date (Monday/Tuesday) up to Thursday 5:00 PM should be restricted. However, if a user selects a Wednesday or Thursday in the following weeks or a month from the current date, they should be able to proceed.


Any idea how the restriction could be implemented in a Business Rule? We already know how the scheduling works, just the restrictions.

 

if FridayDeadline.isInSchedule(CreatedDateTime){
     if start_date is before next Tuesday 5:00pm
             then error, abort
      else
             success
}

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@symonflores_23 

try this in before update business rule

Create Script Include:to calculate the next Tuesday at 5:00 PM from a given date:

var DateUtils = Class.create();
DateUtils.prototype = {
    initialize: function() {},

    getNextTuesdayAt5PM: function(currentDate) {
        var date = new GlideDateTime(currentDate);
        var dayOfWeek = date.getDayOfWeek(); // 1 = Sunday, 7 = Saturday

        // Calculate days to add to reach next Tuesday
        var daysToAdd = (9 - dayOfWeek) % 7; // 9 because Tuesday is 3rd day of the week and we need to add 7 days if it's already Tuesday

        date.addDaysLocalTime(daysToAdd);
        date.setDisplayValue(date.getDisplayValue().split(' ')[0] + ' 17:00:00'); // Set time to 5:00 PM

        return date;
    },

    type: 'DateUtils'
};

business rule: Condition: Your date/time field changes

(function executeRule(current, previous /*null when async*/) {
    var dateUtils = new DateUtils();
    var nextTuesdayAt5PM = dateUtils.getNextTuesdayAt5PM(new GlideDateTime());

    var startDate = new GlideDateTime(current.start_date);

    if (startDate.before(nextTuesdayAt5PM)) {
        gs.addErrorMessage('The start date must be after next Tuesday at 5:00 PM.');
        current.setAbortAction(true);
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@symonflores_23 

try this in before update business rule

Create Script Include:to calculate the next Tuesday at 5:00 PM from a given date:

var DateUtils = Class.create();
DateUtils.prototype = {
    initialize: function() {},

    getNextTuesdayAt5PM: function(currentDate) {
        var date = new GlideDateTime(currentDate);
        var dayOfWeek = date.getDayOfWeek(); // 1 = Sunday, 7 = Saturday

        // Calculate days to add to reach next Tuesday
        var daysToAdd = (9 - dayOfWeek) % 7; // 9 because Tuesday is 3rd day of the week and we need to add 7 days if it's already Tuesday

        date.addDaysLocalTime(daysToAdd);
        date.setDisplayValue(date.getDisplayValue().split(' ')[0] + ' 17:00:00'); // Set time to 5:00 PM

        return date;
    },

    type: 'DateUtils'
};

business rule: Condition: Your date/time field changes

(function executeRule(current, previous /*null when async*/) {
    var dateUtils = new DateUtils();
    var nextTuesdayAt5PM = dateUtils.getNextTuesdayAt5PM(new GlideDateTime());

    var startDate = new GlideDateTime(current.start_date);

    if (startDate.before(nextTuesdayAt5PM)) {
        gs.addErrorMessage('The start date must be after next Tuesday at 5:00 PM.');
        current.setAbortAction(true);
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader