- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 08:00 AM
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);
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 08:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 08:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 08:33 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 08:45 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 10:03 AM
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;
},