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

Abhinay Erra
Giga Sage

Can you post your script include here, I can help you out.


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;


Can you post your client script here ? Are you using evalJson() in the client script? Is this in a scoped application?


No. It is not in scoped App. Below is my client script code.



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




                answer2 = answer.evalJSON();



  alert(answer2.org);



  g_form.setValue('u_up_department_name',answer.dept_name,true);


  g_form.setValue('u_up_organization',answer.org,true);



  }


Is this on your personal developer instance? If yes I can look into it.