Completion date must be minimum 15 business days from today

Dave_p
Giga Guru

Hi,

 

 I have  a requirement where the date field should not allow user to select next 15 business days. I want to use only Server Side code. Please don't post the links. Kindly help.

 

The schedule sys_ID from the schedules is 090eecae0a0a0b260077a9dfa71db2e3

 

Regards

Suman P.

1 ACCEPTED SOLUTION

This script validates whether the date in a record’s date_field is at least 15 business days in the future. It uses a predefined schedule (identified by schId) to calculate 15 business days from the current date, considering working hours and excluding weekends or holidays. If the provided date is earlier than this calculated threshold, an error message is displayed, and the save or update action is aborted. This ensures that the chosen date aligns with the organization’s business day requirements.

(function executeRule(current, previous) {
    // Define the schedule ID. This points to a specific schedule in ServiceNow.
    var schId = "090eecae0a0a0b260077a9dfa71db2e3";
    
    // Create a GlideSchedule object using the schedule ID to work with business days.
    var sch = new GlideSchedule(schId);
    
    // Get the current date and time.
    var now = new GlideDateTime();
    
    // Add 15 business days to the current date based on the schedule.
    var next15Days = sch.add(now, 15, "business");
    
    // Extract only the date part (ignoring the time) from the calculated date.
    var dateThreshold = new GlideDateTime(next15Days).getDate();
    
    // Check if the date in the 'date_field' is earlier than the threshold date.
    if (current.date_field < dateThreshold) {
        // If it is, add an error message to the UI.
        gs.addErrorMessage("Date must be after 15 business days.");
        
        // Prevent the record from being saved or updated.
        current.setAbortAction(true);
    }
})(current, previous);

 

View solution in original post

11 REPLIES 11

Hi @yuvarajkate,

 

Initially, I tried using the Before Business Rule only. It didn't work, and it still doesn't. I revert it to Before Business Rule, still no success.

 

Regards

Suman P.

I should work as expected. See if any other component or anything is causing it to not work as expected.