Auto populate a date after 2 business days from current date in catalog form

sireesha09
Tera Contributor

I have a date field in the catalog form. I want the date field to be auto-populated to a date after 2 business days from current date. The script should only consider week days. It should throw error when user tries to odify the date less than 2 business days from current date. I tried onchange client script and script include, also script in default value from community, but nothing worked.

8 REPLIES 8

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Can you share what you tried and is not working?

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Catalog client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //Show error when weekend is selected
    var ga = new GlideAjax("RMBTSelectWeekDays");
    ga.addParam("sysparm_name", "isWeekDay");
    ga.addParam("sysparm_date", newValue);
    ga.getXML(CallBack);

    function CallBack(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'true') {
           
            g_form.showFieldMsg('date_you_ll_be_onsite_to_set_this_device', 'Select any weekday', 'error', true);
        }
    }
 
script include:
var RMBTSelectWeekDays = Class.create();
RMBTSelectWeekDays.prototype = {
    initialize: function() {
    },
isWeekDay: function() {
        var selected_date = new GlideDateTime(this.getParameter('sysparm_date'));
        var today = new GlideDateTime();

        var dur = new DurationCalculator();
        // loads "8-5 weekdays excluding holidays" schedule

        dur.setSchedule('090eecae0a0a0b260077e1dfa71da828');
        dur.calcScheduleDuration(today, selected_date);
        var secs = dur.getSeconds();
        var days = secs / (9 * 60 * 60);
        if (days < 2) {
            return 'true';
        }
        return 'false';
    },

    type: 'RMBTSelectWeekDays'
};

Hi Sireesha,

I have tried to using schedule for excluding weekends and holidays to auto populate the date field after 7 days based on current date .but couldn't work.

Please let me know if it is worked fine ?

Yashsvi
Kilo Sage

Hi @sireesha09,

Step 1: Client Script :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var selectedDate = new GlideDateTime(newValue);
    var currentDate = new GlideDateTime();
    
    // Calculate the date after 2 business days
    var businessDays = 0;
    var daysToAdd = 2; // Change this value if you want more than 2 business days

    while (businessDays < daysToAdd) {
        selectedDate.addDays(1);
        if (!isWeekend(selectedDate.getDayOfWeek())) {
            businessDays++;
        }
    }

    // Set the calculated date back to the field
    g_form.setValue(control.name, selectedDate.getValue());

    // Validation: Prevent selection of dates less than 2 business days from current date
    var minDate = new GlideDateTime();
    minDate.addDays(daysToAdd);

    if (selectedDate.compareTo(minDate) < 0) {
        g_form.clearValue(control.name);
        g_form.showErrorBox(control.name, "Please select a date at least 2 business days from today.");
    }
}

function isWeekend(dayOfWeek) {
    // 1 = Sunday, 7 = Saturday
    return dayOfWeek != 1 && dayOfWeek != 7;
}

Step 2: Business Rule:

(function executeRule(current, previous /*null when async*/) {
    var currentDate = new GlideDateTime();
    var businessDays = 0;
    var daysToAdd = 2;

    while (businessDays < daysToAdd) {
        currentDate.addDays(1);
        if (!isWeekend(currentDate.getDayOfWeek())) {
            businessDays++;
        }
    }

    current.date_field = currentDate;
})(current, previous);

Thank you, please make helpful if you accept the solution.