The Zurich release has arrived! Interested in new features and functionalities? Click here for more

onchange Client script is not working for the script include

pavam
Tera Contributor

Hi, MY catalog form

 

pavam_0-1726233182887.png

 

 

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.

 

 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;
    },


        BusinessUnits : function(BusinessGroup){
         gs.log(" level4 business Unit"+ BusinessGroup );
         
         var unitchoices=[];
         var gr= new GlideRecord('u_ehsvelocities');
         gr.addQuery('u_business_group',BusinessGroup);
         gr.query();
         
         while (gr.next()){
            gs.log(" level4 in while loop"+gr.u_business_unit);
          unitchoices.push (gr.getValue('u_business_unit').toString());
         }
         
          return 'sys_idIN' +unitchoices;
    },
     
       WorldArea : function(BusinessGroup, BusinessUnits){

         gs.log("level5 WorldArea " );
         
         var worldareas=[];
         var gr= new GlideRecord('u_ehsvelocities');
         gr.addQuery('u_business_group',BusinessGroup);
         gr.addQuery('u_business_unit',BusinessUnits);
         gr.query();
         while (gr.next()){
            gs.log(" level5 in while loop"+gr.u_world_area);
         worldareas.push (gr.getValue('u_world_area').toString());

   
         }
         
          return 'sys_idIN' +worldareas;
   
    },  

               
   getCountry : function(){

         gs.log("level6 Country " );

         var businessGroup=this.getParameter('sysparm_business_group');
          var businessUnits=this.getParameter('sysparm_business_unit');
          var worldArea=this.getParameter('sysparm_word_area');

         var buflexCountry=[];
         var gr= new GlideRecord('u_ehsvelocities');
         gr.addQuery('u_business_group',businessGroup);
         gr.addQuery('u_business_unit',businessUnits);
         gr.addQuery('u_world_area',worldArea);
         gr.query();
         while (gr.next()){
            gs.log(" level6 in while loop"+gr.u_bu_flex);
         buflexCountry.push(gr.getValue('u_bu_flex').toString());

         }
         
          //return 'sys_idIN' +buflexCountry;
          return buflexCountry.join(',');
 
 
 and below is my client script :
 
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 alert("1");
   //Type appropriate comment here, and begin script below
   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;
    }
  alert("before entering the loop");
    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){
       
        alert("in response");
        var buflexCountry=response.split(',');
        g_form.clearOptions('u_bu_flex');
        g_form.addOption('u_bu_flex','','--select Country--');
         alert("ibefore for");
        for(var i=0;i<buflexCountry.length;i++){
            g_form.addOption('u_bu_flex',buflexCountry[i],buflexCountry[i]);
            alert("in for");

        }
        alert("after for");
    });
    
}
 
 
 and custom table is
 
pavam_1-1726233954029.png

 

 

and the fields in the Service catalog is 

 

pavam_2-1726234046106.png

 

 

 

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.

 
 
5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

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.

 

Siddhesh Gawade
Mega Sage
Mega Sage

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

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

Sandeep Rajput
Tera Patron
Tera Patron

@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.

pavam_1-1726233954029.png

addOption will only work with select box and not with a single line text.

 

Hope this helps.