Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Date validation if date is 7 days from today

shaik23
Tera Expert

Hello mates,

 

Below is my requirement.

 

Create a catalog client script that runs when the ‘Is this due to be returned at a specific date?’ field is updated.

Generate an error if the Date is not 7 days from today

 

 

for this i have created a catalog client script script given below but its not working as expected, i am missing logic can anyone please help me.

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

    // Calculate the valid date (7 days from today)
    var validDate = new Date(today);
    validDate.setDate(today.getDate() + 7);

    // Get selected date
    var selectedDate = new Date(newValue);
    selectedDate.setHours(0, 0, 0, 0);

    // Compare the dates directly (Date vs Date)
    if (selectedDate.getTime() !== validDate.getTime()) {
        g_form.showFieldMsg('is_this_due_to_be_returned_at_a_specific_date',
            'Date must be exactly 7 days from today', 'error');
    } else {
        g_form.hideFieldMsg('is_this_due_to_be_returned_at_a_specific_date', true);
    }



}

 

shaik23_0-1763043826348.png

shaik23_1-1763043835124.png
Date format is month/date/year in our instance

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@shaik23 

why not use catalog UI policy and no scripting required.

this will handle the different date formats as well

No Code date validations through (Catalog) UI Policies 

AnkurBawiskar_0-1763044014892.png

AnkurBawiskar_1-1763044025355.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @shaik23 

You can try with a UI policy, and it is easy to do."

 

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

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

shaik23
Tera Expert

@Ankur Bawiskar @Dr Atul G- LNG  thanks will try but my client said to use client script, ui policy is best fit than client script i am not sure why they asked for client script !

Hi @shaik23 

 

If you check this link, you’ll notice that a UI Policy is 100% low-code/no-code, which means it’s easy to maintain and introduces no technical debt. So, suggest to the client that using a UI Policy would be the better approach in this case.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************