Skip navigation

Developer Community

1 Post authored by: Timothy Harris

A while back I realized that we were running into what I would call, "script include sprawl." Let me explain...

 

Pretty much every GlideAjax call that was being made had it's own corresponding script include to return specific data. At best, a new method was being added to an already existing script include. YUCK!! Think about it-- It doesn't take long for things to get really messy.

 

Then, I got the ACE report results and realized that there were tons of client scripts that were doing direct GlideRecord queries instead of a GlideAjax call. Things were about to get even messier unless I provided a standardized way to do all GlideAjax calls through one script include.

 

Here is the solution I came up with:

 

var AjaxGlideRecord = Class.create();
AjaxGlideRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getRecordField: function() {
  var data = '';
  var table = this.getParameter('sysparm_table_name')+'';
  var enc = this.getParameter('sysparm_encoded_query')+'';
  var field = this.getParameter('sysparm_field')+'';

  var gr = new GlideRecord(this.getParameter('sysparm_table_name'));
  gr.addEncodedQuery(this.getParameter('sysparm_encoded_query'));
  gr.query();
  if(gr.next()) {
  data = gr.getValue(field);
  }
  return data;
  },
  getRecordFields: function() {
  var data = {};
  var table = this.getParameter('sysparm_table_name')+'';
  var enc = this.getParameter('sysparm_encoded_query')+'';
  var fields = this.getParameter('sysparm_fields')+'';
  var field_names = fields.split("|");

  var gr = new GlideRecord(this.getParameter('sysparm_table_name'));
  gr.addEncodedQuery(this.getParameter('sysparm_encoded_query'));
  gr.query();
  if(gr.next()) {
   field_names.forEach(function(element) {
   data[element] = gr.getValue(element);
   });  
  }
  data = new global.JSON().encode(data);
  return data;
  },
    type: 'AjaxGlideRecord'
});

 

Here is how to retrieve a single piece of data from a client script:

 

var ga = new GlideAjax('AjaxGlideRecord');
ga.addParam('sysparm_name','getRecordField');
ga.addParam('sysparm_table_name',"sys_user");
ga.addParam('sysparm_encoded_query',"sys_id=ba1443d38db62400932b553d6ead9dec");
ga.addParam('sysparm_field',"email");
ga.getXML(parseResult);


function parseResult(response) {
   var answer = response.responseXML.documentElement.getAttribute("answer");
   console.log(answer);
}

 

Here is how to retrieve multiple pieces of data from a client script:

 

var ga = new GlideAjax('AjaxGlideRecord');
ga.addParam('sysparm_name','getRecordFields');
ga.addParam('sysparm_table_name',"sys_user");
ga.addParam('sysparm_encoded_query',"sys_id=ba1443d38db62400932b553d6ead9dec");
ga.addParam('sysparm_fields',"email|user_name");
ga.getXML(parseResult);


function parseResult(response) {
   var answer = response.responseXML.documentElement.getAttribute("answer");
   answer = answer.evalJSON(); // Turn JSON string into object
   console.log(answer.email);
   console.log(answer.user_name);
}

 

Problem solved.

Filter Blog

By date: By tag: