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.

Catalog item Problem

Teo Gamarra
Tera Expert

I have the following issue in Catalog Items.


I have a 
select box variable type that has a default value ("Please Select One") and type specification is pointing to a tableColumn with multiple choices(picture attached).

I have set up a CCScript onLoad to confirm that I'm actually selecting a choice and not the 
default value from the requester side. However, I feel there should be a better approach for this since the variable value can change and script won't work accordingly, any advice? - CC Script attached. 

 

TeoGamarra_0-1763602566736.png

 

 

function onSubmit() {
    var loc = g_form.getValue('library_location');

    // All allowed values (from sys_choice for Library Location)
    var validValues = [
        'Main Library',
        'Branch A',
        'Branch B'
        // etc… every real option, but NOT "please_select_one"
    ];

    if (validValues.indexOf(loc) === -1) {
        g_form.showFieldMsg(
            'library_location',
            'Please select a valid library location.',
            'error'
        );
        return false;
    }

    return true;
}

 

1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

The recommendation is to not create a choice value that should not be selected so you don't have to prevent it from being selected.  Just use the out of box functionality "Include none" on the Type Specifications, no Default Value, and make the variable mandatory if you want to ensure a selection is made.

 

If you stick with this approach for whatever reason, your onSubmit script should simply show the message and return false if the variable value is "Please Select One" - so you don't have to maintain the script, but this is not a great user experience.