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

Looks like this will work on a field in a table, my requirement is to auto populate on a catalog form variable

Rupanjani
Giga Guru

Hi @sireesha09,

 

Below scripts are to set date after 2 business days.

Client Script (onLoad/onChange - as required):

 

 

var gaChng = new GlideAjax('Test'); //script include name
    gaChng.addParam('sysparm_name', 'setDate'); //second parameter - function name in the SI
    gaChng.getXML(getData2);

    function getData2(response) {
        var answer2 = response.responseXML.documentElement.getAttribute('answer');
        alert(answer2);

        if (answer2) {
            g_form.setValue('<field_name>', answer2); //Replace <field_name> with actual field name 
        }
    }

 

 

Script Include (make sure client callable is true):

 

 

setDate: function() {
        try {
            var gdt = new GlideDateTime();
            if (gdt.getDayOfWeekLocalTime() == 5 || gdt.getDayOfWeekLocalTime() == 4) { //Fri --> Tue & Thu --> Mon
                gdt.addDays(4);
            } else if (gdt.getDayOfWeekLocalTime() == 6) { //Saturday --> Tuesday
                gdt.addDays(3);
            } else { //Sun to Wed --> after 2 days
                gdt.addDays(2);
            }

            gs.info('-->> returnDate:' + gdt.getValue());
            return gdt.getValue();
        } catch (ex) {
            gs.info('-->> Error:' + ex.string() + '\nLine:' + ex.lineNumber);
        }
    },

 

 

 


Mark the response correct and helpful if the answer assisted your question.

Rupanjani
Giga Guru

Hi @sireesha09 , 

 

For the other ask, where error message should be shown when the date is modified to before 2 days.

This conflicts with the date that is set on the form and shown error, as soon as the date field is auto populated.

 

Eg: When the form is opened, lets assume the date field is set with the value '2024-07-11 17:50:33'. By the time it is set and the onChange Client Script runs and checks the value, the 2 days check will be atleast '2024-07-11 17:50:34'. Due to the difference in seconds, the error msg could be shown.


Mark the response correct and helpful if the answer assisted your question.

Community Alums
Not applicable

Hi @sireesha09 ,

 

Can you please try the below code once-

Create a client callable script include-

var BusinessDayUtils = Class.create();
BusinessDayUtils.prototype = {
    initialize: function() {},

    getDateAfterBusinessDays: function(days) {
        var gdt = new GlideDateTime();
        var businessDays = 0;

        while (businessDays < days) {
            gdt.addDays(1);
            var dayOfWeek = gdt.getDayOfWeekLocalTime(); // 1 = Monday, 7 = Sunday
            if (dayOfWeek != 6 && dayOfWeek != 7) { // Skip Saturday (6) and Sunday (7)
                businessDays++;
            }
        }

        return gdt.getValue();
    },

    type: 'BusinessDayUtils'
};

 

Onload client script-

function onLoad() {
    var ga = new GlideAjax('BusinessDayUtils');
    ga.addParam('sysparm_name', 'getDateAfterBusinessDays');
    ga.addParam('sysparm_days', '2');
    ga.getXMLAnswer(function(response) {
        var dateAfter2BusinessDays = response;
        g_form.setValue('your_date_field', dateAfter2BusinessDays);
    });
}

 

Onchange Client script-

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

    var ga = new GlideAjax('BusinessDayUtils');
    ga.addParam('sysparm_name', 'getDateAfterBusinessDays');
    ga.addParam('sysparm_days', '2');
    ga.getXMLAnswer(function(response) {
        var dateAfter2BusinessDays = new GlideDateTime(response);
        var selectedDate = new GlideDateTime(newValue);

        if (selectedDate.compareTo(dateAfter2BusinessDays) < 0) {
            g_form.showErrorBox('your_date_field', 'Please select a date at least 2 business days from today.');
            g_form.clearValue('your_date_field');
        }
    });
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar