- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2016 02:26 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2016 12:18 AM
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);
...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2016 02:57 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2016 04:10 PM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2016 05:20 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2016 12:18 AM
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);
...