I need a catalog client script for my scenario.

Taaha M
Tera Contributor

In my scenario I need to validate date and time for a variable as per below: 

if value is less than 24hrs from current date, display field message:
"Request is less than 24 hours from time of arrival. Fulfillment may be delayed."

3 REPLIES 3

Deepak Shaerma
Kilo Sage

Hi @Taaha M 
Select “onSubmit” if you want to validate the field when the form is submitted. Alternatively, you can choose “onChange” if the validation should occur immediately when the date and time field changes. Adjust the script according to your requirement. 

 var currentDate = new GlideDateTime(); // Get current date and time
    var selectedDate = new GlideDateTime(newValue); // Create GlideDateTime object from the selected date and time

    // Calculate the difference in milliseconds
    var timeDifference = GlideDateTime.subtract(currentDate, selectedDate);

    // Convert milliseconds to hours
    var hoursDifference = timeDifference.getNumericValue() / (1000 * 60 * 60);

    // Check if the difference is less than 24 hours
    if (hoursDifference > -24 && hoursDifference < 0) {
        var message = "Request is less than 24 hours from time of arrival. Fulfillment may be delayed";
        g_form.showFieldMsg('requested_date_time', message, 'info', false); // Adjust the ‘requested_date_time’ to your variable’s name
    }

 

Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma

 

Abhishek_Thakur
Mega Sage

Hi @Taaha M ,

 

If you need to validate the date difference, you can use below code to fulfil your requirement.

 

var currentDate = new GlideDateTime();
if(currentDate<=24){
    var text = "Request is less than 24 hours from time of arrival. Fulfillment may be delayed";
    g_form.showFieldMsg('requested_date_time', text, 'info', false);
}
 
Please mark this helpful if it works for you.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Taaha M 

 

Greetins!!

 

Did you try with UI policy with relative as operator?

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

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