Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get all the choice values in a script

anusha60
Kilo Expert

Hi,

I am trying to get all the choice option values in a client script to verify whether the choice field has a particular choice option in it,if not I want to create that option dynamically.

Could someone suggest me,whether we have any method to get the list of choice values in script.

Thanks in advance......

Thanks,

Anusha.

5 REPLIES 5

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Anusha,



You can try this client script by changing <choicefield> to the name of your choice field.



var values = [];


var sel = g_form.getElement('<choicefield>');


for (var i=0, n=sel.options.length;i<n;i++) {


  if (sel.options[i].value) values.push(sel.options[i].value);


}


alert(values.join(","));


Thank you Brad, it was helpful for me.



Thanks.


Anusha.


You can also achieve this by the below piece of script



var field = gr.getElement('os');


var choices = field.getChoices();


Saad8
Giga Contributor

getControl() wont work for the portal though. And I think it is not a good idea to play around with the HTML elements as g_form.getControl() does. I did it like Naveen demonstrated below and sent the values over to the client by a 'g_scratchpad' value using a display business rule.