Date Validation

spandanaait
Giga Contributor

Date validation should ensure the value is within 12 months from the current date.

Users have different date format preferences (e.g., YYYY-MM-DD, MM/DD/YYYY), leading to script failures.

Script works for some users and fails for others due to format mismatch.

2 ACCEPTED SOLUTIONS

VijayKumarDodde
Tera Contributor

Always convert the date to a standard format (e.g., yyyy-MM-dd) in your script, regardless of the user preference.

 

// Example: Convert input date to standard format (in GlideScript)

var inputDate = new GlideDateTime(current.your_date_field); 

var formattedDate = inputDate.getDisplayValue(); // or use inputDate.getValue() for system format (yyyy-MM-dd)

 

Then perform your validation logic using GlideDateTime

 

Example: 

 

// Assume 'your_date_field' is the date field you want to validate

 

(function executeRule(current, gS, gRequest, gResponse, gProcessor, gs) {

    

    // Get the date from the record (ALWAYS use getValue() to avoid user format issues)

    var inputDate = new GlideDateTime(current.your_date_field.getValue());

    

    // Get today's date

    var today = new GlideDateTime();

    

    // Calculate date 12 months ago

    var pastDate = new GlideDateTime();

    pastDate.addMonthsUTC(-12); // subtract 12 months from today

    

    // Validation logic: check if inputDate is between pastDate and today

    if (inputDate.before(pastDate) || inputDate.after(today)) {

        gs.addErrorMessage("Date must be within the last 12 months.");

        current.setAbortAction(true); // Stops the insert/update

    }

 

})(current, g_scratchpad, g_request, g_response, g_processor, gs);

View solution in original post

@spandanaait 

I believe I also shared a correct and good answer with no-code approach.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

View solution in original post

4 REPLIES 4

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @spandanaait 

So you want to test if the date falls within 12 months? You can use a UI Policy to handle that."

DrAtulGLNG_0-1754399627024.png

 

*************************************************************************************************************
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]

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

Ankur Bawiskar
Tera Patron
Tera Patron

@spandanaait 

you should use UI policy for this and it will take care of the date format and you need not worry on it.

No Code date validations through (Catalog) UI Policies 

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

@spandanaait 

I believe I also shared a correct and good answer with no-code approach.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

VijayKumarDodde
Tera Contributor

Always convert the date to a standard format (e.g., yyyy-MM-dd) in your script, regardless of the user preference.

 

// Example: Convert input date to standard format (in GlideScript)

var inputDate = new GlideDateTime(current.your_date_field); 

var formattedDate = inputDate.getDisplayValue(); // or use inputDate.getValue() for system format (yyyy-MM-dd)

 

Then perform your validation logic using GlideDateTime

 

Example: 

 

// Assume 'your_date_field' is the date field you want to validate

 

(function executeRule(current, gS, gRequest, gResponse, gProcessor, gs) {

    

    // Get the date from the record (ALWAYS use getValue() to avoid user format issues)

    var inputDate = new GlideDateTime(current.your_date_field.getValue());

    

    // Get today's date

    var today = new GlideDateTime();

    

    // Calculate date 12 months ago

    var pastDate = new GlideDateTime();

    pastDate.addMonthsUTC(-12); // subtract 12 months from today

    

    // Validation logic: check if inputDate is between pastDate and today

    if (inputDate.before(pastDate) || inputDate.after(today)) {

        gs.addErrorMessage("Date must be within the last 12 months.");

        current.setAbortAction(true); // Stops the insert/update

    }

 

})(current, g_scratchpad, g_request, g_response, g_processor, gs);