addoption and removeoption methods are not working on choice list in client script?

swamyk
Mega Expert

Hi Guys,

I created a onchange client script for adding and removing the choice list field value dynamically by using addoptin and remove option methods. but they are not working up to the expectation. below is the code,

Table:catalog_task

type:onchange

field:status

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

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

  return;

  }

  if (newValue == '-5'){

  var substatus=g_form.getValue('sc_task.request_item.u_item_summary');

  alert('Item : ' + substatus);

  var st=g_form.getValue('state');

  var num=substatus.indexOf('*');

  alert('num : ' + num);

  if(num>-1)

  {

  g_form.addOption('u_sub_status','Onstatus','Onstatus',3);//not working

  alert('done');

  }

  else if(num=='-1')

  {

  alert('num : ' + num);

  g_form.removeOption('u_sub_status', 'Onstatus');//not working

  alert('done');

  }

  }

  //Type appropriate comment here, and begin script below

}

Note: sub status field is dependent on the status field and i tried with ui policy as well but no luck.

Any help much appreciated.

Thanks in advance.

1 REPLY 1

nehruvj
Giga Contributor

Please refer the below script which is working fine for us:



function onLoad() {


  var gr = new GlideRecord('sys_choice');


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


  gr.addQuery('element','category');


  gr.query(recResponse);


}




function recResponse(rec) {


  var incView = g_form.getViewName();


  if(incView == 'Incident1'){


  while (rec.next()) {


  if (rec.value.indexOf('tsc_'))


  g_form.removeOption('category',rec.value);


  }


  }


  if(incView == 'Incident2'){


  while (rec.next()) {


  if (rec.value.indexOf('src_'))


  g_form.removeOption('category',rec.value);


  }


  }


}



To help you better, would like to know more on the functionality of your script:



a. What is the response on 1st Alert statement on your script <alert('num : ' + num);>


b. Also would like to know the response of <alert('num : ' + num);>