Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Restrict user selecting past date and time in catalog.

WaseemM
Tera Expert

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', '');
    }
}

 

1 ACCEPTED SOLUTION

I didn't know that we could do this with UI Policy.

I just made it as shown below according to my requirements. Thanks!

 

UI Action.pngUI Policy.png

View solution in original post

5 REPLIES 5

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @WaseemM 

There is no need to write this error—you can use the Catalog UI Policy and restrict the past-date selection.

 

DrAtulGLNG_0-1765448892628.png

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]

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

I didn't know that we could do this with UI Policy.

I just made it as shown below according to my requirements. Thanks!

 

UI Action.pngUI Policy.png

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_Branscombe
Tera Expert

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);
}