onchange Client script is not working for the script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 06:28 AM
Hi, MY catalog form
Based on level3 value, level 4 choice list values will be populated.
Based on level4 value, level 5 choice list values will be populated.
Based on level5 value, level 6 choice list values will be populated.
Below is the script include.
and the fields in the Service catalog is
level4 , the field type i have given is Select box, to populate the list of values on that field.
I dont know , where i have made mistake.
method in Script include is executing , but the result is not updating on the Service catalog form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 07:07 AM - edited 09-13-2024 07:51 AM
There's a lot going on here that's going to be hard to follow without replicating it. You've got logs in the SI which is a good start. Add one before the return on buflexCountry.join(',') to confirm that is has a value to pass back to the client, and that it contains the expected values. In the Client Script, you are trying to clear then add options to a variable named u_bu_flex, but that's not shown in your screenshots. Is this variable name correct? It seems like it should be the ridiculously long level 5 name. Do yourself and everyone who follows a favor and rename those variables to something like level_3, level_4, level_5, and level_6. These need to be select box variables since you are attempting to addOption. I don't know if you can set the Label on the 'none' choice to something other than -- None --, so you might need to change this line to
g_form.addOption('u_bu_flex','','-- None --');
You are showing the onChange script attempting to pass level3 and level4 values to the SI, so this must be a test case and script onChange of the level4 variable. Because of the first if condition, the script won't run if this variable is empty, so you can simplify your return to not run if level3 is empty
if (!level3BusinessGroup) {
Also know that once you get this specific test case working, you'll need similar onChange scripts when level3 and level5 change.
You should also add logs to the SI to confirm that the parameters passed in from the client are received and the expected values to work in the GlideRecord.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 07:18 AM
Hello @pavam ,
In the glideAjaxCall you are passing below two variables as string value.
ga.addParam('sysparm_business_group','level3BusinessGroup');
ga.addParam('sysparm_business_unit','level4Businessunit');
Instead you as should passign them as below:
ga.addParam('sysparm_business_group',level3BusinessGroup);
ga.addParam('sysparm_business_unit',level4Businessunit);
You can try below revised version on client script( I haven't tested it):
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var level3BusinessGroup = g_form.getValue('location_of_role_assignment_as_in_the_location_tree_level_3');
var level4Businessunit = g_form.getValue('location_of_role_assignment_as_in_the_location_tree_level_4');
if (!level3BusinessGroup || !level4Businessunit) {
return;
}
var ga = new GlideAjax('GetBusinessUnitsByBusinessGroup');
ga.addParam('sysparm_name', 'getCountry');
ga.addParam('sysparm_business_group', level3BusinessGroup);
ga.addParam('sysparm_business_unit', level4Businessunit);
ga.addParam('sysparm_word_area', newValue);
ga.getXMLAnswer(function(response) {
if (response) {
var buflexCountry = response.split(',');
g_form.clearOptions('u_bu_flex');
g_form.addOption('u_bu_flex', '', '--None--');
for (var i = 0; i < buflexCountry.length; i++) {
g_form.addOption('u_bu_flex', buflexCountry[i], buflexCountry[i]);
}
} else {
// alert('nothing');
}
});
}
Also, Ensure that the response you’re getting from the server is formatted as expected. Since you’re splitting the response with ,, confirm that the server-side script is returning a comma-separated string.
Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.
Regards,
Siddhesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 11:33 PM
Hi Siddesh,
I changed the code as per ur suggestion, but no luck,
after removing the codes, it is not even entring into the scriptinclude

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 07:35 AM
@pavam couple of observations from my side here.
BusinessGroup : function(){
gs.log(" level3 in si"+level3)
var choices=[];
var gr= new GlideAggregate('u_ehsvelocities');
gr.groupBy('u_business_group');
gr.query();
while (gr.next()){
gs.log(" level3 in si in loop"+gr.u_business_group);
choices.push (gr.getValue('u_business_group').toString());
}
//return JSON.stringify(choices);
return 'sys_idIN' +chocies;
},
In this script, I found a type at the line
return 'sys_idIN' +chocies;
This will crash the script, replace it with the following.
return 'sys_idIN' +choices;
The BU Flex field (u_bu_flex) looks more like a Single line text field to me in the screen shot than a select box.
addOption will only work with select box and not with a single line text.
Hope this helps.