Unable to populate 4th working day field in native UI

Manindra Harsha
Tera Contributor

function onLoad () 

//Type appropriate comment here, and begin script below

// Return date string in format YYYY-MM-DD

function getISODate (date) {

function z (n) {

return (n < 10? '0': ') + n;

return date. getFullYear () + '- + z(date.getMonth) + 1) + '-• + z(date.getDate();

/ Modifies date by adding 3 days, excluding sat and sun

function add3WorkingDays (date) {

for (var i = 4; i; i--) {

/ Add a day

date. setDate date. getDate() + 1);

/I If a weekend or holiday, keep adding until not

while (! (date. getDay () % 6)) {

date. setDate (date getDate()

+ 1);

return date;

/I Add 3 working days to today

var d = new Date();

g_form.setValue(date_needed1', addWorkingDays (d)) ;

}

I want to populate the next 4th working day( exclude Saturday and Sundays). For e.g today date was 26-10-2023 so the next 4th working day was 01-11-2023 .So I used this ON LOAD script. It was working in service portal but it was not working in Native UI. So please suggest me why it was not working in native UI.

1 ACCEPTED SOLUTION

Anand Kumar P
Giga Patron
Giga Patron

Hi @Manindra Harsha ,
Check the client script for 'ui type' set to 'all,'
Setting  the 'date_needed' field to the date that is 4 working days from the current date, excluding Saturdays and Sundays.

 

function onLoad() {
    // Return date string in format YYYY-MM-DD
    function getISODate(date) {
        function z(n) {
            return (n < 10 ? '0' : '') + n;
        }
        return date.getFullYear() + '-' + z(date.getMonth() + 1) + '-' + z(date.getDate());
    }

    // Modifies date by adding 4 working days, excluding Sat and Sun
    function add4WorkingDays(date) {
        for (var i = 0; i < 4; i++) {
            do {
                date.setDate(date.getDate() + 1);
            } while (date.getDay() === 0 || date.getDay() === 6); // Keep adding until it's not Sat or Sun
        }
        return date;
    }
    // Add 4 working days to today
    var d = new Date();
    g_form.setValue('date_needed', getISODate(add4WorkingDays(d)));
}

 

Please mark as helpful and solution proposed if it serves your purpose.

Thanks,

Anand

View solution in original post

2 REPLIES 2

Anand Kumar P
Giga Patron
Giga Patron

Hi @Manindra Harsha ,
Check the client script for 'ui type' set to 'all,'
Setting  the 'date_needed' field to the date that is 4 working days from the current date, excluding Saturdays and Sundays.

 

function onLoad() {
    // Return date string in format YYYY-MM-DD
    function getISODate(date) {
        function z(n) {
            return (n < 10 ? '0' : '') + n;
        }
        return date.getFullYear() + '-' + z(date.getMonth() + 1) + '-' + z(date.getDate());
    }

    // Modifies date by adding 4 working days, excluding Sat and Sun
    function add4WorkingDays(date) {
        for (var i = 0; i < 4; i++) {
            do {
                date.setDate(date.getDate() + 1);
            } while (date.getDay() === 0 || date.getDay() === 6); // Keep adding until it's not Sat or Sun
        }
        return date;
    }
    // Add 4 working days to today
    var d = new Date();
    g_form.setValue('date_needed', getISODate(add4WorkingDays(d)));
}

 

Please mark as helpful and solution proposed if it serves your purpose.

Thanks,

Anand

Hi @Anand Kumar P 

Thanks for your solution it was working fine now. This means a lot