Fetch values from JSON.parse(answer);

SNOW32
Giga Expert

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.

find_real_file.png

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]);
            
    }

1 ACCEPTED SOLUTION

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);
            
    }

View solution in original post

10 REPLIES 10

Hi Harshavardhan,

You are great!! Its working 🙂 thank you so much your help 🙂 ... But one thing i need to ask ...in choice list i am not getting --None-- value. Its coming as empty. see the screenshot.

find_real_file.png

Regarding,var ci = this.getParameter('sysparam_caller');  ..as u suggested I tried sysparm_caller also. but its giving null value. So i didn't update it

I am concerned about one more thing...I selected any choice options and save the form. After that rest of the options are vanished.Only selected option and None is there in choice lists... please see the screenshot below:

 

find_real_file.png

If user want to again update any choice option?

 

Thanks,

Swathi

if you are adding those option using client script then it wont be available after saving the form. client script is just for form .

eg: if you seeing 2 drop down values and selected one , that will be only available after saving the form other will be vanished.  

 

if i am not wrong you client script is working on field change  so you can run your script on load as well. 

 

just comment the below line in your client script then those option will be available on form load. 

 

// if (isLoading || newValue === '') {
// return;
// }

 

can you confirm in the field dictionary you have selected the choice as "drop down with None"

 

find_real_file.png

Hi Harshavardhan,

Yes, My choice field is "Drop down with None" only. Still

find_real_file.png

Thanks,

Swathi

I tried to update service name field to "drop down with None" as well as "drop down without None" but an empty space is coming in choice list which should not be there. Please tell me what I need to change in code.

thanks,

swathi

can you confirm, on "u_service_names", how many number of values stored ? is it also storing blank value ?