The CreatorCon Call for Content is officially open! Get started here.

Ui script

Affanahmadd
Tera Contributor

create a  ui script

task State>>Mumbai,Delhi,Uttar Pradesh

City>>Juhu,Bandra,Dadar,Okhla,Jafrabad,Cp,Lucknow,fatehpur,kanpur

when we select state=mumbai then,

city=juhu,bandra,dadar

when we select state=delhi  then,

city=Okhla,Jafrabad,Cp

when we select state=Uttar Pradesh then,

city=Lucknow,fatehpur,kanpur

2 REPLIES 2

AshishKM
Kilo Patron
Kilo Patron

HI @Affanahmadd ,

Where are you using these two select box based field, how are you adding state field values, are you using choice  [ sys_chocie ] table or Question Choice table [ question_choice].

 

you can refer the category & sub-category mapping logic from incident form where category and sub-category defined over sys_choice table with dependency. In this case, every city has a state value as dependent.

 

refer this for sample 

https://www.servicenow.com/community/developer-articles/dependent-select-box-choice-variables-on-rec... 

 

refer for sample UI script

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

 

-Thanks,

AshishKMishra


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Amit Gujarathi
Giga Sage
Giga Sage

HI @Affanahmadd ,
I trust you are doing great.
Please find the below code for the same

function updateCityOptions(state) {
    var cityField = g_form.getReference('city'); // Replace 'city' with the actual field name
    cityField.options.length = 0; // Clear existing options

    var cities = {
        'Mumbai': ['Juhu', 'Bandra', 'Dadar'],
        'Delhi': ['Okhla', 'Jafrabad', 'Cp'],
        'Uttar Pradesh': ['Lucknow', 'Fatehpur', 'Kanpur']
    };

    if (cities[state]) {
        cities[state].forEach(function(city) {
            var opt = document.createElement('option');
            opt.value = city;
            opt.innerHTML = city;
            cityField.appendChild(opt);
        });
    }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi