- 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-05-2014 09:27 PM
How about adding a count variable.
count = 0;
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();
while (vSubcat.next())
{
count ++;
}
if(count == 1)
{
g_form.setValue('u_subcategory', vSubcat.label);
}
Best Practices : Such queries should be avoided in client scripts. Write a script include which will perform all the necessary query you want and pass the result in client script.
- 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-11-2014 05:02 PM
Hi Jim,
Thanks for help to date.
I've found that the script stops working periodically.
If you try creating a few records in quick succession it won't work. If you come back sometime later the script will become responsive again, but then repeats they above cycle.
Any ideas on the issue here?
Cheers
Anthony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2014 02:47 AM
It's because it's "client side script", it could depend from a lot of factors.
When I want to be sure about the data populated, I sometimes add a "business rule" as a "safe net" who does the same work than the client script (but the business rule shoudn't erase the data entered by the user/client scripts).
Regards,