- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2024 09:27 AM - edited ‎05-20-2024 09:51 AM
I'm trying to create an onChange Catalog Client Script that will display a modal pop up when someone enters a date that is within 30 days from the current date. I've tried using a number of scripts posted on community but they are not working for me. We do not want to use a UI Policy/Action for this. Any help with a script to achieve this would be greatly appreciated!
- Variable = u_correction_begin_date
- If u_correction_begin_date is within 30 days of current date they should receive a modal pop up indicating "you cannot submit a date within 30 days of today."
- If u_correction_begin_date is 30 days or more, no pop up should appear.
Thank you in advanced!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-20-2024 10:29 AM - edited ‎05-20-2024 10:30 AM
Hi @Valerie24 ,
Create an onchange client script and try the below code
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var currentDate = new Date();
var selectedDate = new Date(g_form.getValue('u_correction_begin_date'));
// Calculate the difference in days
var timeDiff = selectedDate - currentDate;
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Convert milliseconds to days
// Check if the selected date is within 30 days of the current date
if (parseInt(diffDays)<30 && parseInt(diffDays)>=0) {
alert('Please select a date that is at least 30 days from today.');
g_form.clearValue('u_correction_begin_date'); // Optionally clear the field
}
}
Try this and let me know if it works
Regards
Vengadesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2024 09:59 AM
Hi @Valerie24
Just now saw your reply, thank you for reaching out
Hope you got the solution from (Keshav07)!!
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-22-2024 08:09 AM
Would this work if I had a similar need, but the 2 fields are variables?
Here is what we have
Date leaving variable
Date returning variable
The days between the 2 cannot exceed 30
Is there a way to do that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2024 08:46 AM
To achieve your desired behavior in a ServiceNow Catalog Client Script using the onChange event, here is a script that will trigger a modal pop-up when the entered date is within 30 days of the current date:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Get the current date and the entered date
var currentDate = new Date();
var enteredDate = new Date(newValue);
// Calculate the difference in time (milliseconds)
var timeDifference = enteredDate.getTime() - currentDate.getTime();
// Convert the time difference to days
var dayDifference = Math.ceil(timeDifference / (1000 * 3600 * 24));
// Check if the date is within 30 days
if (dayDifference < 30) {
// Show the modal pop-up if within 30 days
g_form.showFieldMsg('u_correction_begin_date', 'You cannot submit a date within 30 days of today.', 'error');
} else {
// Clear any previous field messages if applicable
g_form.clearMessages();
}
}
Explanation:
- The script listens for changes in the u_correction_begin_date field.
- It calculates the difference between the current date and the entered date.
- If the difference is less than 30 days, a modal pop-up will appear, preventing submission.
For further detailed insights on date-related calculations or custom plugins like the one you're trying to achieve, feel free to explore additional resources or examples on my website 30 Days From Today. There, you'll find useful tools for date calculations and more.