I need to return values in an array

nomadie
Kilo Expert

Hi, I have this script include that I need to return values in an array but I am noob in scripting. Anyone can review my work? Thanks.

Expected result should look like this
  Infrastructure - 3
  Network - 5

stdCHG: function(cat, sortBy) {
		
		var recs = [];		
		var aggSTD = new GlideAggregate('change_request');
        aggSTD.addAggregate('COUNT', 'type');
		aggSTD.addEncodedQuery('stateIN4,5,6,7^start_date<=javascript:gs.endOfToday()^end_date>=javascript:gs.beginningOfToday()^type=Standard');
        aggSTD.groupBy('category');
        aggSTD.query();
        while (aggSTD.next()) {
            //do things on the results
            var openSTDCount = aggSTD.getAggregate('COUNT', 'type');
            var cat = aggSTD.category.getDisplayValue();
            gs.info(cat + " = " + openSTDCount);
            return (cat + " " + openSTDCount);
        }
1 ACCEPTED SOLUTION

Suseela Peddise
Kilo Sage

Hi,

Try below:

 

var recs = [];
var aggSTD = new GlideAggregate('change_request');
aggSTD.addAggregate('COUNT', 'type');
aggSTD.addEncodedQuery("stateIN4,5,6,7^start_date<=javascript:gs.endOfToday()^end_date>=javascript:gs.beginningOfToday()^type=Standard");
aggSTD.groupBy('category');
aggSTD.query();
while (aggSTD.next()) {
//do things on the results
var openSTDCount = aggSTD.getAggregate('COUNT', 'type');
var cat = aggSTD.u_category.getDisplayValue();
gs.info(cat + " = " + openSTDCount);
var res=cat + " = " + openSTDCount;
recs.push(res);
// return (cat + " " + openSTDCount);
}
//gs.print(recs);

return recs;

 

If I have answered your question, please mark my response as correct and/or helpful.

Thanks,

Suseela P.

View solution in original post

6 REPLIES 6

And of course, you could even pass in the Change Type as a parameter as well:

change: function(changeType, fieldName) {
    var result = [];
    var ga = new GlideAggregate("change_request");
    ga.addAggregate("COUNT", "type");
    ga.addEncodedQuery('stateIN4,5,6,7^start_date<=javascript:gs.endOfToday()^end_date>=javascript:gs.beginningOfToday()^type=' + changeType);
    ga.groupBy(fieldName);
    ga.orderBy(fieldName);
    ga.query();
    while (ga.next()) {
        //add the results to the array
        result.push(ga[fieldName].getDisplayValue() + " - " + ga.getAggregate("COUNT", "type"));
    }
    return result;
},

Thanks Jim for sharing that too. I have completed work using the shared code. But I have taken notes of this. Thanks.