The CreatorCon Call for Content is officially open! Get started here.

Date should not be in past

Sirri
Tera Guru

Hi All,

 

As per my requirement Date should not be selected as past if select we should want to clear the date.

 

If I do with  UI Policy with Run Script I'm getting but I should want get with On change client script.

 

Please let us know how can i get this requirement with out using ui policy for this

 

Thank you

9 REPLIES 9

Harish Bainsla
Kilo Patron
Kilo Patron

HI you can use ui policy check below video to achieve your requirement

https://youtu.be/ZJrNM-o0MSU?si=5t79pOQhya4oBIXR

Sirri already mentioned having achieved that 😉

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Aniket Chavan
Tera Sage
Tera Sage

Hello @Sirri ,

As @Mark Roethof wisely recommended, sticking with UI policies is often preferred for their simplicity and alignment with ServiceNow's low-code philosophy. If you find that UI policies meet your needs seamlessly, they are a robust choice.

However, I understand your curiosity about alternative solutions. For your specific requirement, an onChange client script can be employed. Below is a sample script to prevent the selection of past dates:

 

 

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

    var currentDate = new Date().getTime();
    var selectedDate = new Date(g_form.getValue('yourDateField')).getTime();

    if (selectedDate < currentDate) {
        var errorMessage = 'Please choose a date equal to or after today.';
        g_form.clearValue('yourDateField');
        g_form.addErrorMessage(errorMessage);
    }
}

 

 


Additionally, if you're interested in exploring a comprehensive article on No-Code date validations through (Catalog) UI Policies, I recommend checking out this enlightening and well-explained piece shared by @Mark Roethof : No Code date validations through (Catalog) UI Policies
And below are the few of YouTube links as well,

Hi @Aniket Chavan ,

 

I have tried with this but it not taking current date.

when ever select current date it will showing as error.

but my requirement is we cannot  select past date only but we can select current date

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @Sirri ,

Please give a try to the script below and see how it works for you and I have tested it from my end and its working as expected for me even if I select the current date it's not throwing an error and it will clear the value of the variable and show the error message only if the selected date is past date.

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

    var currentDate = new Date().setHours(0, 0, 0, 0); // Set time to midnight for accurate comparison
    var selectedDate = new Date(g_form.getValue('yourDateField')).setHours(0, 0, 0, 0);

    if (selectedDate < currentDate) {
        var errorMessage = 'Please choose a date equal to or after today.';
        g_form.clearValue('yourDateField');
        g_form.addErrorMessage(errorMessage);
    }
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket