- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
I have a code block with restricts the user to select past dates, but I'm not able to restrict the past time. For example, my current date and time is 2025-12-11 01:30:00. I want the user to be restricted to pick the time of 2025-12-11 01:25:00, which is the same day but a past time.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '')
return;
var value = g_form.getValue('snapshot_creation_time');
if (!value)
return;
// Convert the snapshot_creation_time to a time object
var snapshotTime = new Date(value);
var currentTime = new Date();
// check if snapshot creation time must be in future
if (snapshotTime <= currentTime) {
g_form.showFieldMsg('snapshot_creation_time', 'Snapshot creation time must be in the future.', 'error');
g_form.setValue('snapshot_creation_time', '');
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
I didn't know that we could do this with UI Policy.
I just made it as shown below according to my requirements. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi @WaseemM
There is no need to write this error—you can use the Catalog UI Policy and restrict the past-date selection.
and in script section add error message
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
I didn't know that we could do this with UI Policy.
I just made it as shown below according to my requirements. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
You used UI policy only mate.
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
I assume your variable is a Date/Time type? If so, you could try using getDateFromFormat as described here. Example based on your use-case below:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var nowDateTime = new Date().getTime();
var selectedDateTime = getDateFromFormat(newValue, g_user_date_time_format);
var isFutureDateTime = (nowDateTime < selectedDateTime);
alert('isFutureDateTime: ' + isFutureDateTime);
}