- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2019 06:26 AM
Hi All,
As I am beginner in coding I am not able to fetch choices option in for loop when using JSON.parse(answer); Getting wrong result(below) in comma separated and in single line. It should be like options.
How to achieve it? Please let me know what to modify in my code. Here is my code below:
var obj = JSON.parse(answer);
alert(answer);
g_form.clearOptions('u_service_name');
for(var i=0; i<obj.length; i++)
{
g_form.addOption('u_service_name', obj[i],obj[i]);
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2019 07:07 AM
try now.
var ci = this.getParameter('sysparam_caller');
var gr = new GlideRecord('u_cx_service_name');
gr.addQuery('u_configuration_item', ci);
gr.query();
var arrReturn = [];
while (gr.next())
{
var obj = {};
obj.lab = gr.getDisplayValue('u_service_names'),
obj.val =gr.getValue('u_service_names')
arrReturn.push(obj);
}
return JSON.stringify(arrReturn);
var obj = JSON.parse(answer);
alert(answer);
g_form.clearOptions('u_service_name');
for(var i=0; i<obj.length; i++)
{
g_form.addOption('u_service_name', obj[i].val,obj[i].lab);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2019 06:35 AM
i am assuming here you have fetched choice label and value in json , validate your server side code, in which object you have stored the label and value .
in server side your script should be like,
var obj ={};
obj.lab = gliderecordobject.label;
obj.val =gliderecordobject.value;
in client script
var obj = JSON.parse(answer);
alert(answer);
g_form.clearOptions('u_service_name');
for(var i=0; i<obj.length; i++)
{
g_form.addOption('u_service_name', obj[i].value,obj[i].lab);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2019 07:00 AM
Hi Harshvardhan,
My script include code is below. It returns value of choices in comma separated manner
var ci = this.getParameter('sysparam_caller');
var gr = new GlideRecord('u_cx_service_name');
gr.addQuery('u_configuration_item', ci);
gr.query();
var arrReturn = [];
while (gr.next())
{
arrReturn.push( gr.u_service_names.getDisplayValue().toString());
}
return JSON.stringify(arrReturn);
In client script , alert(answer) is showing like below:
Please help how to do it.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2019 07:07 AM
try now.
var ci = this.getParameter('sysparam_caller');
var gr = new GlideRecord('u_cx_service_name');
gr.addQuery('u_configuration_item', ci);
gr.query();
var arrReturn = [];
while (gr.next())
{
var obj = {};
obj.lab = gr.getDisplayValue('u_service_names'),
obj.val =gr.getValue('u_service_names')
arrReturn.push(obj);
}
return JSON.stringify(arrReturn);
var obj = JSON.parse(answer);
alert(answer);
g_form.clearOptions('u_service_name');
for(var i=0; i<obj.length; i++)
{
g_form.addOption('u_service_name', obj[i].val,obj[i].lab);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2019 07:20 AM
change this line as well , it's wrong.
var ci = this.getParameter('sysparam_caller');
updated line.
var ci = this.getParameter('sysparm_caller');