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.

Pass Object data from Script Include to Client Script

Srinivas Balusu
Kilo Guru

Hi,

I need to pass the data from an Element on form to bring the data from a table. For that I have created a client script which calls script includes. But how can I bring the entire record from script include to my client script?

1 ACCEPTED SOLUTION

Hi bsss,



You would probably get better results from your "script include/AJAX object" if you move things around a bit and also create an array to stuff results in. Maybe something similar to this:



getDepartment: function() {


  var dept_id = this.getParameter('sysparm_deptid');


  var depts = []; //Array to stuff dept objects for multiple results



  var grd = new GlideRecord('u_cm_departments');


  grd.addQuery('sys_id',dept_id);


  grd.query();


  while (grd.next()){


var dept={};   // Create the object within the while loop



dept.dept_name=grd.u_department_name;


dept.org=grd.u_organization;



              depts.push(); // push results in the array



  }


var   json = new JSON(); // move your json setup outside the while loop


var dept_data = json.encode(depts);//JSON formatted string



return dept_data;



Then in the client script:



var dept_id=g_form.getValue('u_department');


//alert(dept_id);



var ga=new GlideAjax('GetDataFromServer');


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


ga.addParam('sysparm_deptid',dept_id);


ga.getXML(getDepartmentData);



  function getDepartmentData(response) {


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


    alert(answer); //alert to see the string value


    var answer2 = answer.evalJSON():



  alert(answer2[0].org); //alert the first object in the array



  g_form.setValue('u_up_department_name',answer2[0].dept_name,true);


  g_form.setValue('u_up_organization',answer2[0].org,true);



  }



Of course this can be modified too without an array if you know you will always get one result back





------


Edit: Above I forgot to actually push "debt" object into debts array:


...


while(grd.next()){


...


        debts.push(dept);



...


View solution in original post

9 REPLIES 9

mazhar4
Giga Expert

Hi Srini,



You need to use JSON for returning objects from Script Includes.



In your Script Include use the below


var json = new JSON();


var data = json.encode(object);



method and in your client Script use answer = answer.evalJSON();



A very good explanation can be found below:



https://fruitionpartners.eu/blog/2015/11/17/glideajax-return-multiple-values-using-json/



PS. Mark correct or helpful if this helps.


It works fine. But data conversion to JSON is convertign the object to null. Can you please check this.





getDepartment: function() {


  var dept_id = this.getParameter('sysparm_deptid');



  var dept={};



  var grd = new GlideRecord('u_cm_departments');


  grd.addQuery('sys_id',dept_id);


  grd.query();


  while (grd.next()){


  dept.dept_name=grd.u_department_name;



  dept.org=grd.u_organization;



  var json = new JSON();


  var dept_data = json.encode(dept);//JSON formatted string


      }



        return dept_data;


Hi Srini,



Can you put the JSON encode lines outside the while loop and try?





getDepartment: function() {


  var dept_id = this.getParameter('sysparm_deptid');



  var dept={};



  var grd = new GlideRecord('u_cm_departments');


  grd.addQuery('sys_id',dept_id);


  grd.query();


  while (grd.next()){


  dept.dept_name=grd.u_department_name;



  dept.org=grd.u_organization;


      }




  var json = new JSON();


  var dept_data = json.encode(dept);//JSON formatted string




        return dept_data;


}


Hi bsss,



You would probably get better results from your "script include/AJAX object" if you move things around a bit and also create an array to stuff results in. Maybe something similar to this:



getDepartment: function() {


  var dept_id = this.getParameter('sysparm_deptid');


  var depts = []; //Array to stuff dept objects for multiple results



  var grd = new GlideRecord('u_cm_departments');


  grd.addQuery('sys_id',dept_id);


  grd.query();


  while (grd.next()){


var dept={};   // Create the object within the while loop



dept.dept_name=grd.u_department_name;


dept.org=grd.u_organization;



              depts.push(); // push results in the array



  }


var   json = new JSON(); // move your json setup outside the while loop


var dept_data = json.encode(depts);//JSON formatted string



return dept_data;



Then in the client script:



var dept_id=g_form.getValue('u_department');


//alert(dept_id);



var ga=new GlideAjax('GetDataFromServer');


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


ga.addParam('sysparm_deptid',dept_id);


ga.getXML(getDepartmentData);



  function getDepartmentData(response) {


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


    alert(answer); //alert to see the string value


    var answer2 = answer.evalJSON():



  alert(answer2[0].org); //alert the first object in the array



  g_form.setValue('u_up_department_name',answer2[0].dept_name,true);


  g_form.setValue('u_up_organization',answer2[0].org,true);



  }



Of course this can be modified too without an array if you know you will always get one result back





------


Edit: Above I forgot to actually push "debt" object into debts array:


...


while(grd.next()){


...


        debts.push(dept);



...