Glide AJAX returning [object, object]

brostoff17
Tera Contributor

I have a client script and script include to return a users info. I have to do this because the department field is a reference field on the user table.   However, my script is returning [object, object] for all the variables.

What is wrong here?

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

      if (isLoading || newValue === '') {

              return;

      }

     

      var ga = new GlideAjax('RequesterInfo');

      ga.addParam('sysparm_name','requesterInfo');

      ga.addParam('sysparm_preferred_first_name', g_form.getValue('u_requester_name'));

      ga.getXML(showMessage);

     

      function showMessage(response) {

              var answer = response.responseXML.documentElement.getAttribute("answer");

              answer = answer.evalJSON(); //Transform the JSON string to an object

             

              g_form.setValue('u_requester_department', answer.var1);

              g_form.setValue('u_requester_email', answer.var2);

              g_form.setValue('u_requester_telephone', answer.var3);

             

      }

}

Script Include:

var RequesterInfo = Class.create(); RequesterInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

     

      requesterInfo: function() {

              var user = this.getParameter('sysparm_preferred_first_name');

             

              var gr = new GlideRecord('sys_user');

              gr.addQuery('sys_id',user);

              gr.query();

              gs.log (gr.getRowCount() + 'records found');

              if(gr.next()){

                     

                      var obj = {};

                      obj.var1 = gr.department.name;

                      obj.var2 = gr.email;

                      obj.var3 = gr.u_extension;

                     

                      var json = new JSON();

                      var answer = json.encode(obj);//JSON formatted string

                     

                      return answer;

                     

              }

             

             

              type: 'RequesterInfo'

             

      }});

6 REPLIES 6

So, You can use an Object datatype to build your information, but if you need return this information, frist you need using this method JSON.stringify(Object), in the Script Include.

After then, in the function of the Cliente Script, you need use the JSON.pase(Answer), in client script.

Adilson
Tera Contributor

This help-me while traning Glide Ajax, Thanks.