How to use glideAggregate in client script

sowmyaj
Giga Expert

Hi All,

     

        In incident table I have column "abc" and "ijk".   "ijk" is a dependent field on "abc". So, I'm grouping "abc" and getting the count on "ijk". Ex. If I'm grouping "abc" and getting the count of "ijk" I will get 3.

      Also I have a table "XYZ" and "incident". On "XYZ" table I'm writing   a client script. The result I need is when "XYZ" table is loaded I need to aggregate the count of   column "ijk" to get count and compare the count with remaining values and put the highest count in "rst" column of "XYZ" table.

How can I achieve this?

I tried for sample purpose:

Working

var inc = new GlideRecord('incident');

inc.query(incResponse);

function incResponse(inc) {

while(inc.next()) {

  alert("hi");

}

}

For getting count I tried but not working.

var inc = new GlideAggregate('incident');

inc.addQuery('active', 'true');

inc.addAggregate('COUNT', 'category');

inc.query(incResponse);

function incResponse(inc) {

  while (inc.next()) {

    var category = inc.category;

    var categoryCount = inc.getAggregate('COUNT', 'category');

  alert("category "+category, "categoryCount "+categoryCount);

}

}

Can any one help me in resolving this.

Thanks,

Sowmya.

1 ACCEPTED SOLUTION

Mike Allen
Mega Sage

You can do it in a GlideAJAX:



Client Script:



function onLoad() {


  //Type appropriate comment here, and begin script below


  var ajax = new GlideAjax('getIncCount');


  ajax.addParam('sysparm_name', 'getCount');


  ajax.getXML(IncCount);



  function IncCount(response){


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


  alert(answer);


  }


}



Script Include:


Client Callable: true


var getIncCount = Class.create();


getIncCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getCount: function(){


  var inc = new GlideAggregate('incident');


  inc.addQuery('active', 'true');


  inc.addAggregate('COUNT', 'category');


  inc.query();


  var answer = '';


  while (inc.next()) {


  var category = inc.category;


  var categoryCount = inc.getAggregate('COUNT', 'category');


  answer += "\ncategory " + category + ", categoryCount " + categoryCount;


  }


  return answer;


  },


  type: 'getIncCount'


});



Result:


Capture.PNG


View solution in original post

4 REPLIES 4

Mike Allen
Mega Sage

You can do it in a GlideAJAX:



Client Script:



function onLoad() {


  //Type appropriate comment here, and begin script below


  var ajax = new GlideAjax('getIncCount');


  ajax.addParam('sysparm_name', 'getCount');


  ajax.getXML(IncCount);



  function IncCount(response){


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


  alert(answer);


  }


}



Script Include:


Client Callable: true


var getIncCount = Class.create();


getIncCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getCount: function(){


  var inc = new GlideAggregate('incident');


  inc.addQuery('active', 'true');


  inc.addAggregate('COUNT', 'category');


  inc.query();


  var answer = '';


  while (inc.next()) {


  var category = inc.category;


  var categoryCount = inc.getAggregate('COUNT', 'category');


  answer += "\ncategory " + category + ", categoryCount " + categoryCount;


  }


  return answer;


  },


  type: 'getIncCount'


});



Result:


Capture.PNG


Hi Mike,



                  Thanks a lot......



                    Can't we do all this process in client script without "GlideAjax"?



Thanks,


Sowmya


I could not make it work.   I had to go with GlideAJAX.


GlideAggregate doesn't have a client-side version like GlideRecord. However, even if a client-side version existed, you would still want to use a call-back function. Using GlideAjax ensures that you are following ServiceNow's defined scripting best practices. I'm sure your users will appreciate it