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

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.

Thanks I tested it and it worked...however, I don't need to pass value on my function object right

stdCHG: function(cat, sortBy) { //remove this?

I'm glad it worked .

No need to pass the values, script is not using those parameters anyways.

 

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

Thanks,

Suseela P.

You do not have to, BUT, it would make your function more useful if you did.  You could pass the field you want to group on to the function so it can be reused.  Will save you from writing basically the same function to group by another field.  Because you know it's going to happen, especially since you've now asked the question. 🤓

Here's the way I would write it, using a parameter and variable names that are easier to read/understand:

standardChange: function(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=Standard');
    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;
},