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.

JSON.parse is not working is out put showing [object object]

Abhishek Padol1
Tera Contributor

Client script onchange

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading) {
      return;
   }
   var ga = new GlideAjax('Assest_details');
   ga.addParam('sysparm_name','details');
   ga.addParam('sysparm_user',newValue);
   ga.getXMLAnswer(getdata);
   
   function getdata(response){
    g_form.addInfoMessage(response);
    var passval = JSON.parse(response);
    g_form.setValue('u_name',passval.name);
    g_form.setValue('u_model',passval.model);
    g_form.setValue('u_serial_number',passval.serial);
    g_form.setValue('u_assest_tag',passval.tag);

   }
   


   //Type appropriate comment here, and begin script below
   
}
 
Script include:-
var Assest_details = Class.create();
Assest_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    details:function()
    {
        var caller = this.getParameter('sysparm_user');
        var obj ={};
        var gr = new GlideRecord('alm_asset');
        gr.addQuery("assigned_to",caller);
        gr.query();
        if(gr.next()){
            obj.name = gr.display_name.getDisplayValue();
            obj.model = gr.model.getDisplayValue();
            obj.serial = gr.serial_number.getDisplayValue();
            obj.tag = gr.asset_tag.getDisplayValue();
        }
        return JSON.stringify(obj);
        },

    type: 'Assest_details'
});
1 REPLY 1

Robbie
Kilo Patron
Kilo Patron

Hi @Abhishek Padol1,

 

So this is interesting. I've 'copy and pasted' both your Client Script and Script Include on my PDI (Personal Dev Instance) and adjusted the client script accordingly from a test perspective to update just the one field.

It works as expected and sets the value.

 

On trying to break test the code, I looked at unchecking the 'Client callable' checkbox etc on the Script Include but again, this essentially only provides an empty result as you'd expect.

Where do you experience the [object object] result? Is this when setting a value? All values? When displaying theaddInfoMessage() ?

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie