pass entire gliderecord back to client script

rajeeshraj
Tera Guru

How can i pass the entire gliderecord from script include to client script. I am querying location table based on a location name, i would like to get the entire record for the particular location from the scriptinclude back to the client script. Is there an example code?

19 REPLIES 19

Please check if this helps.



Script Include:


var getLocationDetails = Class.create();


getLocationDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getDetails: function(){


var loc_obj = [];


var gr = new GlideRecord('cmn_location');


if(gr.get('sys_id', this.getParameter('sysparm_loc_id'))){


var fields = gr.getFields();


for (var i = 0; i < fields.size(); i++) {


var fieldObj = {};


var field = fields.get(i);


fieldObj.name = field.getName();


fieldObj.value = gr.getDisplayValue(field.getName());


loc_obj.push(fieldObj);


}


var loc_json = new JSON();


var loc_oData = loc_json.encode(loc_obj);


return loc_oData;


}


},


type: 'getLocationDetails'


});





Client Script:


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


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


return;


}


var ga = new GlideAjax('getLocationDetails');


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


ga.addParam('sysparm_loc_id', newValue);


ga.getXML(CallBack);


function CallBack(response){


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


alert(answer);


// var myObj = JSON.parse(answer);


// for (var i = 0; i < myObj.length; i++) {


// alert(myObj[i].name);


// alert(myObj[i].value);


// }


}


}



Client script on Incident table and I am setting short description with city value and Description with country value. (City, Country are fields on cmn_location)


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


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


return;


}


alert(newValue);


var ga = new GlideAjax('test_request');


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


ga.addParam('sysparm_loc',newValue); //This newValue which we are passing to script include must be sys_id of the location.


ga.getXML(getFields);


}




function getFields(response) {



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


answer = answer.evalJSON();




g_form.setValue('short_description',answer.city);


g_form.setValue('description',answer.country);




}










Script Include


var test_request   = Class.create();


test_request .prototype = Object.extendsObject(AbstractAjaxProcessor, {


popLocDetails : function(){


var locat = this.getParameter('sysparm_loc');


var gr = new GlideRecord('cmn_location');


gr.get(locat);



var xmlSerializer = new GlideRecordXMLSerializer();


var taskXML = xmlSerializer.serialize(gr);


var helper = new XMLHelper(taskXML);


var xmlObj = helper.toObject();


var json = new global.JSON().encode(xmlObj);


return json;


},


type: 'test_request '


});





Let me know if you have any issues.


rajeeshraj
Tera Guru

This is what i tried and it is not working



Client Script



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


if (isLoading) {


return;


}



if(newValue==''){


g_form.setValue('phone_sl','');


g_form.setValue('email_sl','');



return;


}



var ga = new GlideAjax('raj');


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


ga.addParam('sysparm_cont',newValue);


ga.getXML(setField);



function setField(response) {



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


answer = answer.evalJSON();


alert('reaching client script'+answer);


g_form.setValue('phone_sl',answer.mobile_phone);


g_form.setValue('email_sl',answer.email);


}



}


********************************************************************************************************************************


Script Include



var raj = Class.create();


raj.prototype = Object.extendsObject(AbstractAjaxProcessor, {



popContactDetails : function(){


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


var glideRecord = new GlideRecord('sys_user');


glideRecord.get(user); //pass the sys_id of the incident you want to pass


var xmlSerializer = new GlideRecordXMLSerializer();


var taskXML = xmlSerializer.serialize(glideRecord);


var helper = new XMLHelper(taskXML);


var xmlObj = helper.toObject();


var json = new global.JSON().encode(xmlObj);alert('Reaching Script Include'+json);


return json;


},


type: 'raj'


});


Hi Rajeesh,



1. On changing which field your onChange script is running? (newValue must be sys_id of the user, because your querying on sys_user table in script include)


2. Are you getting alert 'reaching client script'+answer ?


3. Try gs.log('Reaching Script Include: '+json) in script include. Alert will not work in script include.



Let me know if you have any questions.


did you get a change to look the script which I provided, i tested that and it works for me. Let me know if you get any further help.