- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2014 08:51 PM
Hi,
I have a category and subcategory dependency. I need to create a client side client script that automatically populates the subcategory field if there is only 1 option based on the category that was selected.
See my code below - I know the getRow() function is not valid in a client side client script. Any other suggestions?
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var vCat = g_form.getValue('u_category');
var vSubcat = new GlideRecord ('sys_choice');
vSubcat.addQuery('name', 'u_hr_cases');
vSubcat.addQuery('dependent_value', vCat);
vSubcat.query();
if (vSubcat.getRowCount() == 1){
g_form.setValue('u_subcategory', vSubcat.label);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2014 10:37 PM
You can do it completely client-side with the following onChange script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
setTimeout(function(){
try {
var subCategory = $("incident.subcategory");
if (subCategory.length == 2) { //"-- None --" + another
subCategory.selectedIndex = "1"; //index starts at 0
g_form.setValue("subcategory", subCategory.value); //mark field as changed
}
} catch(err) {}
},500); //half-second wait
}
This example works on the Incident table, setting the Subcategory based on a change to the Category field. You can see it running in demo021 at the moment (select "Test" as the Category). The script waits for half a second then checks the number of entries in the Subcategory drop down. It automatically selects the one option if there are just 2 available ("-- None --" + another). The g_form.setValue call is used to make sure the field is marked as changed with the little green bar.
I don't really like the half-second wait, but it was the best I could come up with at the moment. Maybe someone else knows a better hook into knowing when the Subcategrory field has been repopulated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2015 04:05 PM
Thanks Anthony,
It's works for me as well.
Regards,
AK.