Auto Populate Due date for HR Case with 5 business days.

Community Alums
Not applicable

Hi,

 

If HR case is changes to awaiting acceptance state , Then Auto populate Due date on HR case with 5 Business days from when HR case is changes to awaiting acceptance state.

HR case is closed after 5 business days from when HR case is changes to awaiting acceptance state.

That's why we need to Due date when HR case is closed.

 

Thanks.

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums Please create a business rule on HR Case table as follows.

 

Screenshot 2024-10-08 at 8.11.39 PM.pngScreenshot 2024-10-08 at 8.20.25 PM.png

 

Here is the script.

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    // Function to add business days
    function addBusinessDays(startDate, days) {
        var gdt = new GlideDateTime(startDate);
        var counter = 0;

        while (counter < days) {
            gdt.addDaysLocalTime(1); // Increment the date by 1 day

            // Check if the new day is a weekday (Monday to Friday)
            var dayOfWeek = gdt.getDayOfWeekLocalTime();
            if (dayOfWeek >= 2 && dayOfWeek <= 6) { // 2 = Monday, 6 = Friday
                counter++; // Count only business days
            }
        }
        return gdt;
    }

    // Only set the due date if it is empty
    if (!current.due_date) {
        // Get the current date and add 5 business days
        var dueDate = addBusinessDays(new GlideDateTime(), 5);
        current.due_date = dueDate;
    }


})(current, previous);

Hope this helps.

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

this article talks about how to get and add business days. use similar logic and your schedule and add that in your BR script

Add Business Days 

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

Sandeep Rajput
Tera Patron
Tera Patron

@Community Alums Please create a business rule on HR Case table as follows.

 

Screenshot 2024-10-08 at 8.11.39 PM.pngScreenshot 2024-10-08 at 8.20.25 PM.png

 

Here is the script.

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    // Function to add business days
    function addBusinessDays(startDate, days) {
        var gdt = new GlideDateTime(startDate);
        var counter = 0;

        while (counter < days) {
            gdt.addDaysLocalTime(1); // Increment the date by 1 day

            // Check if the new day is a weekday (Monday to Friday)
            var dayOfWeek = gdt.getDayOfWeekLocalTime();
            if (dayOfWeek >= 2 && dayOfWeek <= 6) { // 2 = Monday, 6 = Friday
                counter++; // Count only business days
            }
        }
        return gdt;
    }

    // Only set the due date if it is empty
    if (!current.due_date) {
        // Get the current date and add 5 business days
        var dueDate = addBusinessDays(new GlideDateTime(), 5);
        current.due_date = dueDate;
    }


})(current, previous);

Hope this helps.