Where will I effectively insert a/the business calendar in this Script

Hola Ola
Giga Guru

Hello all,

I have a requirement to auto-populate an End Date, based on the value of Plan Period (30, 60, or 90 Days) and Start Date
The script below works fine, except that it does not factor the Business Calendar (holidays and weekends) into it's calculations.

 

HOW or WHERE do I insert the Business Calendar?

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

    var pPeriod= g_form.getValue('u_plan_period');

    var pPeriodV= parseInt(pPeriod);

    var pStartDate = new Date(getDateFromFormat(newValue, g_user_date_format));

    //add 5 days to the selected date.

    pStartDate.setDate(pStartDate.getDate() + pPeriodV);

    var pStartDateStr = formatDate(pStartDate, g_user_date_format);

    g_form.setValue('u_plan_end_date',pStartDateStr);

}

 

3 REPLIES 3

SN_Learn
Kilo Patron
Kilo Patron

Hi @Hola Ola ,

 

Check the below post, it will guide you.

Add Business Days 

Calculate the business duration using a schedule 

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

AnirudhKumar
Mega Sage
Mega Sage

You need to use a schedule called 8-5 weekdays excluding holidays

Here's a sample script that adds days with schedule (global application)

var dc = new DurationCalculator();
dc.setStartDateTime(gs.now());  //input present date
dc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //Sysid of schedule: 8-5 weekdays excluding holidays
var durationSeconds = 1*24*60*60; // number of seconds in 1 day
dc.calcDuration(durationSeconds);
//dc.getEndDateTime() gives the present date + 1 day (in datetime format)

 

Here's a sample script that adds days with schedule (scoped application)

var startDate = new GlideDateTime();
var days = 2;
var dur = new GlideDuration(60 * 60 * 9 * 1000 * days);
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');//schedule : 8-5 weekdays excluding holidays
var endDate = schedule.add(startDate, dur);

Sumanth16
Kilo Patron

Hi @Hola Ola ,

 

Please refer to below thread:

https://www.servicenow.com/community/developer-forum/add-a-schedule-lookup-for-client-script-script-...

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks & Regards,
Sumanth Meda