Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Restrict Date Entry on a Variable in a Catalog Item

terrieb
Tera Guru

We have a catalog item that has the variable type of date and the question is Start Date.  

 

We are trying to prevent anyone from inputting today's date as the start date.

 

We have tried the following to prevent this:

 

UI Policy where date can't be at or before today

On the ui Action on the dictionary override record, deselected the mandatory override

Both the UI action and the Variable is set to Mandatory

 

While it works, it only works if someone clicks on that field.  The mandatory symbol does not show up unless they do so and if they try to put in today's date, it won't allow them to submit the request

 

However, if they skip that field, the mandatory symbol does not show up and they can submit the request with that field blank

 

This is critical information so if anyone else has a way to resolve this, we would greatly appreciate it.

 

Thanks!

8 REPLIES 8

Its_Azar
Mega Sage

Hi there @terrieb 

 

I wouldn't rely on a UI Policy to make the variable mandatory in this case. Set the catalog variable itself to Mandatory = true so the form always enforces it. Then use an onSubmit Catalog Client Script to validate that the selected date is greater than today. Finally, add server-side validation in your fulfillment logic or Business Rule, since client-side checks can be bypassed.

 

sample script

function onSubmit() {
    var startDate = g_form.getValue('start_date'); // Replace with your variable name

    if (!startDate)
        return false; // Mandatory field will also prevent submission

    var today = new Date();
    today.setHours(0, 0, 0, 0);

    var selected = new Date(startDate);
    selected.setHours(0, 0, 0, 0);

    if (selected <= today) {
        g_form.showFieldMsg(
            'start_date',
            'Start Date must be after today.',
            'error'
        );
        return false;
    }

    return true;
}
☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.

Kind Regards,
Azar
Serivenow Rising Star
Architect@ KPMG.

Nishant8
Tera Sage

Hello @terrieb , You validation is failing because 'Start Date' variable becomes mandatory only when a user changes the value of field manually.

If I understand the requirement correctly, you would like to make the 'Start Date' variable mandatory while also ensuring that the selected date is not in the past. If so, you can achieve this using the following couple of options:

1. Define 'Start Date' variable itself mandatory while creating it and In existing policy, change/create the UI Policy Action as per below screenshot (Ensure you don't make it mandatory again):

Nishant8_1-1785654754549.png

OR

2. If you don't want to define 'Start Date' variable as mandatory while creating, then create additional UI policy along with the existing one created by you. In existing policy, change/create the UI Policy Action as per below screenshot:

Nishant8_0-1785654390595.png

 

   Below is the description of the new policy

    - Policy Order should be greater than existing one and 'On load' checkbox is selected

    - Condition 'Start Date' is empty

    - UI policy action: select Mandatory as Yes 

This action still did not make the field mandatory when the catalog item was opened.

 

Instead the solution we found to work, is on the variable in the item we added a default date value of 1/1/1900 which is "against" the policy action to not input a date at or before today's date.

 

Now, when the catalog item is opened, that field is marked Mandatory and the error message appears.  The requestor can no longer bypass the field and has to put in a date not equal to the current date or any date prior to current date.

 

terrieb_0-1785760672139.png

 

Hello @terrieb , which option did you follow - 1 or 2? Could you please share the complete screenshots of your UI policies or any other changes you made?

 

Regards,

Nishant