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.

onLoad catalog client script Remove choice from Select box based on date/time variable

booher04
Tera Guru

Trying to remove a choice from a select box variable based on a date/time variable being on or after the first day of the year(ex: January 1, 2025).  

I need to remove the choice "CommVault" from the select box variable "data_restore_environment" if the date/time variable "restore_date" is on or after the first day of this year(January 1, 2025). 

I tried the below with no success:
function onLoad() {
var presentYear = new GlideDateTime().getYear();
var yearStart = new GlideDateTime(presentYear + '-01-01 00:00:00');
var dre = new GlideDateTime(g_form.getValue('restore_date'));

if (dre >= yearStart) {
	g_form.removeOption('data_restore_environment', 'CommVault');
	alert('TEST');
}
}
1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @booher04 ,

 

GlideDateTime() is a server side API and not available in Client Scripts.

Please replace your script with the following:

 

function onLoad() {

    var restoreDate = getDateFromFormat(
        g_form.getValue('restore_date'), g_user_date_time_format);
    var currentYear = new Date().getUTCFullYear();
    var beginningOfThisYear = new Date(currentYear, 0, 1).getTime();

    if (restoreDate >= beginningOfThisYear) {
        g_sc_form.removeOption('data_restore_environment', 'CommVault');
    }
}

 

Please make sure the "Applies on Catalog Tasks" checkbox is enabled. 

 

Regards,

Robert

View solution in original post

6 REPLIES 6

Robert H
Mega Sage

Hello @booher04 ,

 

GlideDateTime() is a server side API and not available in Client Scripts.

Please replace your script with the following:

 

function onLoad() {

    var restoreDate = getDateFromFormat(
        g_form.getValue('restore_date'), g_user_date_time_format);
    var currentYear = new Date().getUTCFullYear();
    var beginningOfThisYear = new Date(currentYear, 0, 1).getTime();

    if (restoreDate >= beginningOfThisYear) {
        g_sc_form.removeOption('data_restore_environment', 'CommVault');
    }
}

 

Please make sure the "Applies on Catalog Tasks" checkbox is enabled. 

 

Regards,

Robert

Thanks for the reply!  The variable "data_restore_environment" only appears on SCTASKs, and the date/time variable "restore_date" is selected on the Portal when the user submits the item.  It won't change after submission.  

Hello @booher04 ,

 

Thanks for the feedback, that's important information.

I have adjusted the script in my original response to work for Catalog Tasks.

 

Regards,

Robert

@Robert H thanks for the quick response.  I tried the script you provided.  It doesn't seem to be working either.  The choice is still displaying on the SCTASK for the select box variable "data_restore_environment".