Add +5 Working Business Day in Workflow Designer [toggle scripting]

ricobuenvia
Tera Contributor

Hi everyone,
I have a question regarding date calculation in my workflow. Right now, I'm using the line addDays(5) in my toggle script to add 5 days to the current date, and it's working as expected. However, the requirement is to add 5 business days, excluding Saturdays and Sundays. Does anyone know help me how to achieve this?

Please refer to the image below for more context. Thanks in advance!

7 REPLIES 7

Chaitanya ILCR
Mega Patron

Hi @ricobuenvia ,

you have to create a new schedule for this( or check if there is already a schedule for weekdays and use it)

ChaitanyaILCR_0-1749607755642.png

 

ChaitanyaILCR_1-1749607770328.pngChaitanyaILCR_2-1749607778261.png

var startDate = new GlideDate();
var days = 7;
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var schedule = new GlideSchedule('2e75b979c3c2e250051cb132b401315b');//replace this with your schedule record sysid
var end = schedule.add(startDate, dur);

gs.info(end);

 

this is the background script used test it in background script and adjust script as per requirement sysid of the schedule should be replaced with your schedule's sysid

gs.info with return statement

 

 

Schedule docs

https://www.servicenow.com/docs/bundle/yokohama-platform-administration/page/administer/time/task/t_...

 

GlideSchedule API docs

https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

thanks for this information Kilo Patron. also checking on this 

thanks for this information @Chaitanya ILCR looking into this also

Ankur Bawiskar
Tera Patron
Tera Patron

@ricobuenvia 

if you are only skipping saturday and sunday and not concerned about public holiday then this script should help you, use this in that toggle script section

// Set your start date (GlideDateTime object)
var startDate = new GlideDateTime(); // Replace with your actual start date
var businessDaysToAdd = 5;
var daysAdded = 0;

while (daysAdded < businessDaysToAdd) {
    startDate.addDaysUTC(1); // Move to the next day
    var dayOfWeek = startDate.getDayOfWeekUTC(); // 1=Monday, 7=Sunday

    // Only count Monday (1) to Friday (5)
    if (dayOfWeek >= 1 && dayOfWeek <= 5) {
        daysAdded++;
    }
}

// Result: startDate now holds the new date after adding 5 business days
gs.info('New date after 5 business days: ' + startDate.getValue());

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