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 Client Script on select box field

Rooma1
Tera Contributor

Hi All,

 

I am trying to create a Catalog Client Script where a Select Box choices are dependent on selection of other Select box options. What is the simplest way of achieving this? I have used OnChange client script but my code isn't working properly.

 

The Select Box fields are:

1. Exception Type

2. Which Security or Quality gate to select

 

Exception Type:

None

Quality Gate

Security Gate

 

Which Security or Quality gate to select: (if Exception Type = Quality Gate)

Code Coverage

 

Which Security or Quality gate to select: (if Exception Type = Security Gate)

Policy Scan

Pipeline Scan

Sandbox Scan

 

Which Security or Quality gate to select: (if Exception Type = None)

None (It should clear the value and change to None)

 

 

Can someone please help as i am new to coding?

1 ACCEPTED SOLUTION

Satishkumar B
Giga Sage
Giga Sage

Hi @Rooma1 

Certainly! Here's a concise version of the resolution for creating a Catalog Client Script in ServiceNow to dynamically populate a Select Box based on another Select Box's selection:

1. Create a Catalog Client Script:
- Navigate to `Service Catalog` > `Catalog Client Scripts`.
- Create a new script targeting your catalog item form.

2. Script Content:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var exceptionType = g_form.getValue('exception_type');
    var gateField = g_form.getControl('which_gate');

    // Clear gate field if exception type is 'None'
    if (exceptionType === 'None') {
        gateField.value = '';
        return;
    }

    // Define options based on exception type
    var gateOptions = [];
    switch (exceptionType) {
        case 'Quality Gate':
            gateOptions = ['Code Coverage'];
            break;
        case 'Security Gate':
            gateOptions = ['Policy Scan', 'Pipeline Scan', 'Sandbox Scan'];
            break;
        default:
            break;
    }

    // Set options for the gate field
    gateField.options.length = 0; // Clear existing options
    gateOptions.forEach(function(option) {
        gateField.options[gateField.options.length] = new Option(option, option);
    });
}


- Replace `'exception_type'` and `'which_gate'` with your actual field names.
- Ensure the script is associated correctly with your catalog item form in ServiceNow.
- Test thoroughly to verify that the options in `which_gate` update correctly based on selections in `exception_type`.

This script changes in `exception_type`, updates `which_gate` dynamically based on the selected option, and clears `which_gate` if `exception_type` is set to 'None'. Adjust field names and logic as per your specific requirements and field configurations in ServiceNow.

--------------------------------------------------------------------------------------------------------------------
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

View solution in original post

5 REPLIES 5

Hi @Satishkumar B , is there a way we can have option -None- placed with the choice options and on top of all selected by default
as well as it should show the field as mandatory with None option selected. is there a way to implement this ?