Universal GlideAjax w/dot-walking (client-side)
Import
·
Jun 27, 2018
·
article
I won't go into a lengthy article, but essentially out of sanity preservation I developed a universal (reusable) GlideAjax script include that does the following:
- Allows the developer to pass any table as a parameter
- Allows the developer to pass an encoded query for that table
- Allows the developer to pass as many fields as they require
- ...and the best part, within the field/variable parameter you can dot-walk to grab additional data
With these two scripts you shouldn't need to write another GlideAjax script include unless you have a unique circumstance.
Script Include
//JAXscript Script Include
var JAXscript = Class.create();
JAXscript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getStuff: function() {
var vars;
var dataset = [];
var sysparmvars = this.getParameter('sysparm_variables');
var variables = sysparmvars.split(",");
var tbl = this.getParameter('sysparm_tbl');
var qry = this.getParameter('sysparm_query');
//apply the table and encoded query
var rec = GlideRecord(tbl);
var rec_qry = qry;
rec.addQuery(rec_qry);
rec.query();
if (rec.next()){
//setup variables
vars = {};
var vr;
var vr_ext;
var new_vr;
var vr_lbl;
var dv;
//loop through user provided variables array to
//generate object output
for(i=0;i < variables.length;i++){
//if the field name has a '-'
if(variables[i].includes("-")){
var v = variables[i];
dv = v.toString();
vr = dv.split("-");
vr_lbl = v.replace('-','_');
//enable dot-walking here
//traverse the field name to dynamically
//create object attributes
for(a=0;a < vr.length;a++){
vr_ext += "."+vr[a];
}
//dot-walking object is simple string
vr_ext = "rec"+vr_ext.replace(undefined,'')+".toString();";
//dot-walk string executed as code by 'eval' function
vars[vr_lbl] = eval(vr_ext);
vr_ext = undefined;
}else{
//if the field name does not have '-'
vr = variables[i];
vars[variables[i]] = rec[vr].toString();
}
}
//package the dataset
dataset.push(vars);
}
var json = new JSON();
var data = json.encode(vars);//JSON formatted string
return data;
},
type: 'JAXscript'
});
Client Script Example
//JAXscript Client Script
//in 'sysparm_variables' you can dot-walk if you use '-'
//ex1: manager-director-name will fetch the manager's director's displayed name
//ex2: manager-director will return the manager's director's sysID
//you can traverse any reference connection you normally can in GlideRecord
//in 'sysparm_tbl' use any table you need
//in 'sysparm_query' use an encoded query, set variables above it and mix those in as well
var jax = new GlideAjax('JAXscript');
jax.addParam('sysparm_name', 'getStuff');
jax.addParam('sysparm_tbl', 'sys_user');
jax.addParam('sysparm_query', 'employee_number=1234567');
jax.addParam('sysparm_variables','name,u_vp-email,manager-name,u_director-name,location-country,location-latitude');
jax.getXML(showMessage1);
function showMessage1(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer); //Transform the JSON string to an object
//uses '_' instead of '-' for values
g_form.setValue('name',answer.name);
g_form.setValue('email',answer.u_vp_email);
g_form.setValue('manager',answer.manager_name);
g_form.setValue('director',answer.u_director_name);
g_form.setValue('loc','Country: ' + answer.location_country + ' Lat: ' + answer.location_latitude);
}
View original source
https://www.servicenow.com/community/developer-articles/universal-glideajax-w-dot-walking-client-side/ta-p/2321259