Issue adding options via client script to Reference type choice list

davewilkerson
Tera Expert

I have a client script that uses GlideAjax to call a script include and return a comma delimited list.

I use that list to create a GlideRecord with an addQuery using IN.

I get back the records I expect and loop through them.

This is where I run into an issue.

On the form I have a control that is a Reference type to a custom table.

Under Choice List Specification, the control is set to "Dropdown with -- None --"

By default it displays all of the records in that table (there are only 7) when you put focus on the field.

find_real_file.png

In the client script I can run this and it removes all of the option in the select

g_form.clearOptions('u_subnet');

when I try to add only the valid options back using the records from the GlideRecord, it will not add any options back.

g_form.addOption('u_subnet', que.sys_id.toString(), que.u_prefix.toString());

find_real_file.png

I have tried it without the toString() function but it does not matter.

Any ideas on why this doesn't work, or if there is a different way to add the options if it is a Reference field vs. a normal Choice List?

Thanks,

Dave

1 ACCEPTED SOLUTION

Melinda,


I have a HI ticket and they referred me to PRB652494 - "Reference Choice list can't add options via addOption()"



Here was his response:



Hello Dave,


My name is Gustavo, and I am the support engineer assigned to assist you with this incident opened on your behalf. The addOption function does not work with reference choice lists as has been documented in PRB652494. I don't have any better options for you but you may want to post on the community to see if anyone else has tried something similar. I am setting this incident in a state of solution proposed. Please let me know if you have any further questions.





I did code a work around,


I created a new string field (CIDR Ref Qualifier) on the form,   I populate that field (CIDR Ref Qualifier) from the GlideRecord with a comma delimited list.


On the Subnet field I set the Reference Qual to javascript:"u_prefixIN" + current.u_cidr_ref_qualifier.toString();



This works on the load of the form, but not in subsequent changes to the field(CIDR Ref Qualifier)



To handle the rebinding based on fields changing, I created a Client script with the following line to force update the binding on the field.



updateChoiceList_u_subnet('', '', '', false);



There is a function for each choicelist to force an update in the genereated javascript on each page.



It is not the way I wanted to get this to work, but it is the best way I have figured out so far.





Thanks for the help with this issue.



Dave Wilkerson


View solution in original post

11 REPLIES 11

rajeshraya
Giga Expert

Can you please post the script you are using? That way I can suggest what is needed.



Rajesh


Rajesh,


Thanks for the assistance.   Here is the section of the script that removes the options and then tries to add specific ones back.


u_subnet is the name of the control.




var results = response.responseXML.documentElement.getAttribute('answer');


  if(results.length > 0)


  {


  g_form.clearOptions('u_subnet');



  var que = new GlideRecord('u_cidr');


  que.addQuery('u_prefix','IN',results);


  que.orderBy('u_prefix');


  que.query();



  while(que.next()) {


  g_form.addOption('u_subnet', que.sys_id.toString(), que.u_prefix.toString());


  }




  }


davewilkerson
Tera Expert

Rajesh,


Thanks for the assistance.   Here is the section of the script that removes the options and then tries to add specific ones back.


u_subnet is the name of the control.




var results = response.responseXML.documentElement.getAttribute('answer');


  if(results.length > 0)


  {


  g_form.clearOptions('u_subnet');



  var que = new GlideRecord('u_cidr');


  que.addQuery('u_prefix','IN',results);


  que.orderBy('u_prefix');  


  que.query();



  while(que.next()) {


  g_form.addOption('u_subnet', que.sys_id.toString(), que.u_prefix.toString());


  }




  }


Hello Dave,



If you were to put an alert('this is an alert') inside the while, BEFORE the addOption(), does the alert message appear when you would expect (when the new options would be added to the u_subnet drop-down)?



If the alert does not appear, then this could be your first clue to a root cause (no records are returned from the query = potential issue with the GlideRecord piece). If you do receive the alert, then try moving the alert after the addOption().... and so on. I use this method often to pinpoint a root cause with a script not functioning as expected.



As a suggested code addition, you may want to add a piece a that checks for returned records and then has an additional addOptions() to add the "--None--" option back into the drop-down. When you clear the options from a drop-down, it also removes the "--None--".



One final note, if this client script is meant to be applied to the Service Portal and none of the above seemed to resolve the issue, the root cause could be that a GlideRecord is being used since the Service Portal tends to like the GlideAjax exclusively.



I hope that this helps,


-Mel