Variables Dependency

Burhan Shah
Tera Contributor

Hello Community,

 

How can I link RITM variables with each other?

 

For example, I have a country variable of UK. It must show the cities of UK only in city variables once the user

Selects UK variable.

 

The variables type are select box.

 

Regards,

Burhan Shah

1 ACCEPTED SOLUTION

Hello @Burhan Shah 

Since there are multiple cities it always good to add an option through script.

  • Write an onchange client script with field set to country.

OnChange catalog client script:

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

     // Clear the City field
    g_form.clearOptions('city');

    // Define a mapping of countries (in lowercase) to their respective cities
    var countryCityMapping = {
        "turkey": [
            { value: "ankara", label: "Ankara" },
            { value: "istanbul", label: "Istanbul" },
            { value: "izmir", label: "Izmir" }
        ],
        "usa": [
            { value: "new_york", label: "New York" },
            { value: "los_angeles", label: "Los Angeles" },
            { value: "chicago", label: "Chicago" },
            { value: "houston", label: "Houston" },
            { value: "phoenix", label: "Phoenix" }
        ],
        "uk": [
            { value: "london", label: "London" },
            { value: "manchester", label: "Manchester" },
            { value: "birmingham", label: "Birmingham" },
            { value: "glasgow", label: "Glasgow" },
            { value: "leeds", label: "Leeds" }
        ]
        // Add more countries and cities as needed
    };

    // Get the selected country
    var selectedCountry = newValue;
    g_form.addInfoMessage(selectedCountry);
    // Add the cities corresponding to the selected country
    if (countryCityMapping[selectedCountry]) {
        var cities = countryCityMapping[selectedCountry];
        g_form.addOption('city', '', '--Select a City--'); // Add a placeholder option
        cities.forEach(function(city) {
            g_form.addOption('city', city.value, city.label);
        });
    } else {
        g_form.addOption('city', '', '--No Cities Available--');
    } 
}

Here I have mapped for three country only, you can map for as many you want.

This script will add an option based on the country selected from the data in our script.

Note: You can set inactive false for all the city and try to populate the data from script.

This is tested in my PDI.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

7 REPLIES 7

Hello @Burhan Shah 

Since there are multiple cities it always good to add an option through script.

  • Write an onchange client script with field set to country.

OnChange catalog client script:

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

     // Clear the City field
    g_form.clearOptions('city');

    // Define a mapping of countries (in lowercase) to their respective cities
    var countryCityMapping = {
        "turkey": [
            { value: "ankara", label: "Ankara" },
            { value: "istanbul", label: "Istanbul" },
            { value: "izmir", label: "Izmir" }
        ],
        "usa": [
            { value: "new_york", label: "New York" },
            { value: "los_angeles", label: "Los Angeles" },
            { value: "chicago", label: "Chicago" },
            { value: "houston", label: "Houston" },
            { value: "phoenix", label: "Phoenix" }
        ],
        "uk": [
            { value: "london", label: "London" },
            { value: "manchester", label: "Manchester" },
            { value: "birmingham", label: "Birmingham" },
            { value: "glasgow", label: "Glasgow" },
            { value: "leeds", label: "Leeds" }
        ]
        // Add more countries and cities as needed
    };

    // Get the selected country
    var selectedCountry = newValue;
    g_form.addInfoMessage(selectedCountry);
    // Add the cities corresponding to the selected country
    if (countryCityMapping[selectedCountry]) {
        var cities = countryCityMapping[selectedCountry];
        g_form.addOption('city', '', '--Select a City--'); // Add a placeholder option
        cities.forEach(function(city) {
            g_form.addOption('city', city.value, city.label);
        });
    } else {
        g_form.addOption('city', '', '--No Cities Available--');
    } 
}

Here I have mapped for three country only, you can map for as many you want.

This script will add an option based on the country selected from the data in our script.

Note: You can set inactive false for all the city and try to populate the data from script.

This is tested in my PDI.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

Chandra18
Mega Sage

Hi @Burhan Shah ,

There are multiple ways:

1.Via Reference qualifier:
https://www.servicenow.com/community/itsm-forum/requirement-is-to-populate-country-and-city-names-in... 

2.Via Client script: https://www.servicenow.com/community/developer-articles/creating-dependent-variables-in-service-cata... 


3. Also, You can create two variable which type is Select box for country & City with dependency value and then create a client script.

Please mark as helpful!

Thank You!
Chandra

Runjay Patel
Giga Sage

Hi @Burhan Shah ,

 

You can do something like below.

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

    // Define country-city mapping
    var cityMapping = {
        'UK': ['london', 'manchester', 'edinburgh'],
        'US': ['new_york', 'los_angeles', 'chicago']
    };

    // Get the City variable field name
    var cityField = 'city';

    // Clear and reset City variable choices
    g_form.clearOptions(cityField);

    // Add a default "Select City" option
    g_form.addOption(cityField, '', '--Select City--');

    // Get the list of cities for the selected country
    var cities = cityMapping[newValue] || [];

    // Add filtered city choices
    cities.forEach(function(city) {
        g_form.addOption(cityField, city, city.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase()));
    });
}

 

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------