I want to have a maxium cutoff date for a date field.

NabilA581920111
Tera Contributor

Hi There,

 

I want to add a date field to a catalog item and want a cut off date where if i fuill in a date before the first of juli or the first of januari it is not possible to fill that date in.

 

How can i achieve this? I want to use a catalog client script or maybe UI policy. 

 

Please assist me! 

4 REPLIES 4

Brad Bowman
Kilo Patron

I don't know if I'm following the test/use case criteria exactly, but try this approach first:

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-... 

SP22
Giga Sage

Hello @NabilA581920111,

To achieve this in ServiceNow, you can add a date field to your catalog item and then use a Catalog Client Script to enforce the cutoff date logic. Here's how you can do it step-by-step:

Add a Catalog Client Script

  1. In the same catalog item, go to the Catalog Client Scripts tab.
  2. Click New.
  3. Set:
    • Type: onChange
    • Variable Name: requested_date
    • Script: Use the following script:
      function onChange(control, oldValue, newValue, isLoading) {
        if (isLoading || newValue == '') {
          return;
        }
      
        var selectedDate = new Date(newValue);
        var year = selectedDate.getFullYear();
      
        var janCutoff = new Date(year, 0, 1);   // January 1st
        var julCutoff = new Date(year, 6, 1);   // July 1st
      
        if (selectedDate < janCutoff || selectedDate < julCutoff) {
          g_form.showFieldMsg('requested_date', 'Date must be after January 1st and July 1st.', 'error');
          g_form.setValue('requested_date', '');
        }
      }​

If this helped to answer your query, please mark it helpful & accept the solution.

Thanks
Santosh.p

svirkar420
Tera Guru

Hi @NabilA581920111 , I had acheived something similar to this from my recent project.

Here is the onChange catalog client script for your requirement.

Script:

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

var selectedDate = new Date(newValue);
selectedDate.setHours(0, 0, 0, 0);

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

var year = today.getFullYear();
var jan1 = new Date(year, 0, 1); // Jan = month 0
var july1 = new Date(year, 6, 1); // July = month 6var nextCutoff;
if (today < july1) {
nextCutoff = july1; // If we are before July, next cutoff is July 1 of this year
} else {
nextCutoff = new Date(year + 1, 0, 1); // If we are past July, next cutoff is Jan 1 of next year
}if (selectedDate < nextCutoff) {
g_form.addErrorMessage("Please select a date on or after " + nextCutoff.toDateString() + ".");
g_form.clearValue('your_date_variable'); // Replace with your actual variable name
}
}

 

If this solution helped you Please Mark this solution as accepted and helpful as it will be helpful for other users as well.
Best Regards.
Saurabh V.

Ankur Bawiskar
Tera Patron

@NabilA581920111 

you can use UI policy with no scripting required.

check link shared by @Brad Bowman  

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader