How to set start field on form to 3 business days from request creation date

Ankitha4
Tera Contributor

Suppose there is a catalog item  webinar request , and on that form we have a field start date . I need to set this field to 3 business days from request creation date.Please help me with the script include and client script for above requirement

4 REPLIES 4

Murthy Ch
Giga Sage

Hello @Ankitha4 

You can below sample code to calculate the business days

var startDate = new GlideDateTime();   //today's date
var days = 3;  //adding 3 business days
var dur = new GlideDuration(60 * 60 * 8 * 1000 * days);     //milli seconds
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae');  //8-5 weekdays schedule sys_id
var end = schedule.add(startDate, dur);   //adding the days
gs.info(end); 

BTW, Did you tried anything? 

Hope it helps

Thanks,
Murthy

Hi @Murthy Ch  , 

 

Thanks for your answer . May I know if I can try this in client script

@Ankitha4 
You cannot directly write the above script in Client Side.

Perform GlideAjax call and do the above calcualtion in script include and return the value to the Client Script.

Let me know if you need more help

Thanks,
Murthy

Harneet Sital
Mega Sage
Mega Sage

Hi @Ankitha4 here is what you can try directly in the client script. The script has been created as onload but feel free to change the type and field names based on your requirement

function onLoad() {
    // Get the current date and time
    var currentDate = new Date();
    
    // Calculate 3 business days from the current date
    var targetDate = addBusinessDays(currentDate, 3);
    
    // Set the 'start_date' field (replace with the actual field name)
    g_form.setValue('start_date', targetDate.toISOString().split('T')[0]);
}

// Function to add business days
function addBusinessDays(startDate, days) {
    var count = 0;
    var currentDate = new Date(startDate);
    while (count < days) {
        currentDate.setDate(currentDate.getDate() + 1);
        // Check if it's a business day (Monday to Friday)
        if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) {
            count++;
        }
    }
    return currentDate;
}

 

-Harneet Sital
Request you please mark my answer as helpful or correct based on the impact
Find all my ServiceNow articles here