I need a catalog client script for my scenario.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 11:37 PM
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."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 11:43 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 12:07 AM
Hi @Taaha M ,
If you need to validate the date difference, you can use below code to fulfil your requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 12:50 AM
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]
****************************************************************************************************************