Regarding date/time variable.

Southsayer
Tera Expert

Hi everyone, 

 

I need to create a date/time type variable.

And in the date picker, the date should be only future dates and allow only upto 90 days. 

 

Thanks in advance for your help! 

1 ACCEPTED SOLUTION

Hi @Southsayer ,

Then you can use the 2 onchange client scripts.

1. Create onchange client script on start date and use below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(g_form.getValue('start_date')); //your start date variable name
        var end = new Date(g_form.getValue('end_date')); //your end date variable name
        if (start > end) {
            g_form.addErrorMessage('Verify start date and end date');
            return false;
        }
    }
}

 

2. Create other onchange client script on end date and use below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(g_form.getValue('start_date')); //your start date variable name
        var end = new Date(g_form.getValue('end_date')); //your end date variable name
        if (end < start) {
            g_form.addErrorMessage('Verify start date and end date');
            return false;
        }
    }
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

11 REPLIES 11

Hi Pavan,

It was helpful. I have two date/time variables start date and end date. 

start date ----- future dates only, allow only up to 90 days and duration up to 90 days.

end date ----- future dates only, allow only up to 90 days and duration up to 90 days.

 

how can I use catalog policy so that the end date should be always greater than the start date and the maximum duration between the start date and end date should be up to 90 days?

 

thanks a lotttt for your help!

Hi @Southsayer ,

1. end date ----- future dates only, allow only up to 90 days and duration up to 90 days.

for above step 1 you need to create UI policy which i shared for start and create another UI policy for End date and use same conditions.

 

2. To do Start date and end date validation below onsubmit client script.

function onSubmit() {
    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(g_form.getValue('start_date')); //your start date variable name
        var end = new Date(g_form.getValue('end_date')); //your end date variable name
        if (end < start) {
            g_form.addErrorMessage('Verify start date and end date');
            return false;
        }
    }
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi Pavan, thanks once again. 

on submit will only work when I submit the request form for the catalog item. however, I need to validate them right away as soon as the end date is entered. How can I do that?

Hi @Southsayer ,

Then you can use the 2 onchange client scripts.

1. Create onchange client script on start date and use below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(g_form.getValue('start_date')); //your start date variable name
        var end = new Date(g_form.getValue('end_date')); //your end date variable name
        if (start > end) {
            g_form.addErrorMessage('Verify start date and end date');
            return false;
        }
    }
}

 

2. Create other onchange client script on end date and use below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(g_form.getValue('start_date')); //your start date variable name
        var end = new Date(g_form.getValue('end_date')); //your end date variable name
        if (end < start) {
            g_form.addErrorMessage('Verify start date and end date');
            return false;
        }
    }
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi,

still no luck. I wonder why it is not working using the catalog on change client scripts. 

Is there any way to do it using the Catalog UI policy? 

thanks in advance....