None in Catalog Client Script After clearOption()

SNOW User8
Giga Guru

Hi Experts,

I am trying to dynamically get "Subcategory" based on "Category" from incident table using catalog client script for Service Portal.

Every time category changes the subcategory getting populated normally. No issues.

But the issue is that getting --None-- value only by the first time category change.

Please find my script below,

Name: Catalog Client Script

function onChange(control, oldValue, newValue, isLoading) {

if(newValue == ''"){

  g_form.clearOptions('subcategory');

  g_form.clearOptions('u_subcategory_2');

  }

if(newValue == oldValue){

  return;

  }

  g_form.clearOptions('subcategory');

  g_form.addOption('subcategory', 'none', '-- None --',0);

  g_form.setValue('subcategory', 'none',   '-- None --');

  var gp = new GlideRecord('sys_choice');      

  gp.addQuery('name', 'incident');      

  gp.addQuery('dependent_value', newValue);      

  gp.addQuery('element', 'subcategory');      

  gp.addQuery('inactive', 'false');      

  gp.query(function(gp) {

  while(gp.next()){

  g_form.addOption('subcategory', gp.value, gp.label,gp.sequence);

  }

  g_form.addOption('subcategory', 'none', '-- None --',0);

  g_form.setValue('subcategory', 'none',   '-- None --');

   

  });    

}

Please help me to solve the issue.

Thanks in Advance.

Regards,

Anna.

17 REPLIES 17

Hi Anna,



Can you post a screen shot? Which table are you using.



Please mark this response as correct or helpful if it assisted you with your question.

Sorry to interject, but the answer is right below here in my post... She's using Catalog Client Scripts and right now, you can't do dependent fields in the Service Catalog without doing what I'm doing below. Your solution only works on the form view.


if we are using record producer, we may need to use field map and set reference qualifier.


I wanted not to use lot of code and utilize out of box script. But there is no other solution, better to go with scripts.



Please mark this response as correct or helpful if it assisted you with your question.

afaik, mapping to field doesn't respect underlying dictionary entries. Also, she's probably already implicitly doing this. If the Question Name is the same value as the database name, then Map to Field is irrelevant as it is implicitly 'checked', a convenient automagic behind the scenes function of SN... Seeing as she's using subcategory and u_subcategory, my guess is that it does map 1 to 1...


akaupisch
Kilo Guru

First, you want to avoid using getReference or GlideRecord in client scripts. Client side scripts should be using ajax calls...it's a major performance issue. That being said, here's how I do it.



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }


  g_form.clearOptions('location');


  var ga = new GlideAjax('RequestUtils');


  ga.addParam('sysparm_name','getLocationsByRegionAJAX');


  ga.addParam('sysparm_id', g_form.getValue('region'));


  ga.getXML(function(response) {


            var locations = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));


            g_form.addOption('location', '', '-- None --');


            for (var i = 0; i < locations.length; i++)


                      g_form.addOption('location', locations[i].sys_id, locations[i].label);


  });



}



then on the serverside script include:


getLocationsByRegion: function(regionID) {




              var gr = new GlideRecord('cmn_location');


              gr.addQuery('parent', regionID);


              gr.query();


              var locations = [];


              while (gr.next()) {


                      var location = {};


                      location.sys_id = gr.sys_id.toString();


                      location.label = gr.name.toString();


                      locations.push(location);


              }




              return locations;




      },




      getLocationsByRegionAJAX: function() {


              return (new global.JSON().encode(this.getLocationsByRegion(this.getParameter('sysparm_id'))));


      },




Oh, and if you really want to use the old way of doing things. Then consider simply doing this:


g_form.clearOptions('subcategory');


g_form.addOption('subcategory', '', '-- None --', 0); // no need to set value as it'll default to this


//your query code


while (gr.next())


g_form.addOption('subcategory', value, label, sequence') // ensure there is no 0 sequence already defined, otherwise it'll be screwy



// again, no reason to setValue, once you clear and set it, it'll default to none.