Date field on catalog item should not allow the past dates

NiloferS
Tera Contributor

NiloferS_0-1718790276248.png

I have written above onchange catalog client script to not allow date field accept past dates. it is not working. kindly suggest.

2 ACCEPTED SOLUTIONS

Paul Curwen
Giga Sage

Here you go, , just change 'delivery_date' to your variable name.

 

Recommendation: try using UI Policies to perform date validation, it's much easier/quicker and can be done without scripting. Take a look at this article: https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 

function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {
        return;
    }

    g_form.hideAllFieldMsgs();
	
    var newDateNumb = dateToNumber(newValue); // converts newValue to an integer as yyyymmdd	
    var todDate = todaysDate();
    var todDateNum = dateToNumber(todDate); // converts todays date to an integer as yyyymmdd	

    if (newDateNumb < todDateNum) {

        g_form.setValue('delivery_date', '');
        g_form.showFieldMsg('delivery_date', 'This date cannot be in the past, please enter another date', 'error');
		return;

    }

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

View solution in original post

Yashsvi
Kilo Sage

Hi @NiloferS,

It looks like you are trying to implement a catalog UI policy in ServiceNow to ensure that a date field on a catalog item does not allow past dates. Here is how you can achieve this:

1. Create a Catalog UI Policy:
- Go to `Service Catalog > Catalog Policies > Catalog UI Policies` in the Application Navigator.
- Click on the `New` button to create a new Catalog UI Policy.

2. Set Conditions:
- In the `When to Apply` tab, set the `Catalog Conditions` as follows:
- Field: `Date` (or the specific date field you are working with)
- Operator: `Relative`
- Comparison: `Before`
- Value: `1 minute ago` (This ensures the date is not in the past)

3. Add UI Policy Actions:
- Go to the `Script` tab to add your custom script.
- Here, you can write a script to set the date field as mandatory or read-only based on the condition.

Here's an example script for the `Script` tab:

function onCondition() {
// Ensure that the date is not in the past
g_form.setReadOnly('your_date_field', true);
g_form.setMandatory('your_date_field', true);
g_form.showFieldMsg('your_date_field', 'The selected date cannot be in the past', 'error');
}

4. Save and Test:
- Save the UI policy and test it by going to the relevant catalog item form to ensure the date field is validated as expected.

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

View solution in original post

4 REPLIES 4

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

You can use a simple no code UI policy for the same, like below

AnuragTripathi_0-1718790675393.png

 

 

In the script part you can clear the variable and throw error.

-Anurag

Paul Curwen
Giga Sage

Here you go, , just change 'delivery_date' to your variable name.

 

Recommendation: try using UI Policies to perform date validation, it's much easier/quicker and can be done without scripting. Take a look at this article: https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 

function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {
        return;
    }

    g_form.hideAllFieldMsgs();
	
    var newDateNumb = dateToNumber(newValue); // converts newValue to an integer as yyyymmdd	
    var todDate = todaysDate();
    var todDateNum = dateToNumber(todDate); // converts todays date to an integer as yyyymmdd	

    if (newDateNumb < todDateNum) {

        g_form.setValue('delivery_date', '');
        g_form.showFieldMsg('delivery_date', 'This date cannot be in the past, please enter another date', 'error');
		return;

    }

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

Trupti94
Tera Guru

Hi @NiloferS ,

 

You can user Catalog UI policy instead of Client Script, Please check below thread.

No Code date validations through (Catalog) UI Policies 

 

Thanks,

Trupti

 

Please consider marking this as helpful/correct if it has been beneficial to you.

Yashsvi
Kilo Sage

Hi @NiloferS,

It looks like you are trying to implement a catalog UI policy in ServiceNow to ensure that a date field on a catalog item does not allow past dates. Here is how you can achieve this:

1. Create a Catalog UI Policy:
- Go to `Service Catalog > Catalog Policies > Catalog UI Policies` in the Application Navigator.
- Click on the `New` button to create a new Catalog UI Policy.

2. Set Conditions:
- In the `When to Apply` tab, set the `Catalog Conditions` as follows:
- Field: `Date` (or the specific date field you are working with)
- Operator: `Relative`
- Comparison: `Before`
- Value: `1 minute ago` (This ensures the date is not in the past)

3. Add UI Policy Actions:
- Go to the `Script` tab to add your custom script.
- Here, you can write a script to set the date field as mandatory or read-only based on the condition.

Here's an example script for the `Script` tab:

function onCondition() {
// Ensure that the date is not in the past
g_form.setReadOnly('your_date_field', true);
g_form.setMandatory('your_date_field', true);
g_form.showFieldMsg('your_date_field', 'The selected date cannot be in the past', 'error');
}

4. Save and Test:
- Save the UI policy and test it by going to the relevant catalog item form to ensure the date field is validated as expected.

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