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.

How to auto populate subcategory based on category field

Usman Farooq
Giga Contributor

I am trying to create functionality to populate the subcategory with the last alphabetical value upon selection of a category. I know it can be done via client script, however not good with scripting and looking for a script I can use?

 

Thank you

1 REPLY 1

Omkar Kadoli
Tera Contributor

Hi Usman,

I think you should try using below code,

I ended up just creating a onChange Client Script, You can add your category & subcategory and try it

 

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

//Type appropriate comment here, and begin script below
var state = g_form.getValue('u_state');

if (state == 'Gujrat') {
g_form.clearOptions('u_city');
g_form.addOption('u_city', 'surat', 'Surat');
g_form.addOption('u_city', 'rajkot', 'Rajkot');
g_form.addOption('u_city', 'amhedabad', 'Amhedabad');
g_form.addOption('u_city', 'gandhinagar', 'Gandhinagar');

}
if (state == 'Karnataka') {
g_form.clearOptions('u_city');
g_form.addOption('u_city', 'chikodi', 'Chikodi');
g_form.addOption('u_city', 'karwar', 'Karwar');
g_form.addOption('u_city', 'belgaon', 'Belgaon');
}
if (state == 'Maharashtra') {
g_form.clearOptions('u_city');
g_form.addOption('u_city', 'pune', 'Pune');
g_form.addOption('u_city', 'mumbai', 'Mumbai');
g_form.addOption('u_city', 'sangli', 'Sangli');
g_form.addOption('u_city', 'kolhapur', 'Kolhapur');

}
if (state == 'Rajsthan') {
g_form.clearOptions('u_city');
g_form.addOption('u_city', 'kota', 'Kota');
g_form.addOption('u_city', 'jodhpur', 'Jodhpur');
g_form.addOption('u_city', 'udaipur', 'Udaipur');
g_form.addOption('u_city', 'jaipur', 'Jaipur');

}
}

 

Untitled.png

 

Thanks,

Omkar