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

SanjivMeher
Kilo Patron
Kilo Patron

Hi Anna,



Instead of writing a client script, you should make category dependent in subcategory field dictionary record.



find_real_file.png



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

Hi Sanjiv Meher,



Thanks for your response,



Having the dependent fields already.


Getting all dependent values in service portal item but the issue is unable to get -- none-- value.



Regards,


Anna.  


Hi Anna,



Do you have the below choice selected for both category and subcategory. If it is, it should work fine.



find_real_file.png



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

Hi Sanjiv Meher,



Yeah ....



Regards,


Anna.