- 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 10:07 AM
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;
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2020 02:39 AM
Thanks Jim for sharing that too. I have completed work using the shared code. But I have taken notes of this. Thanks.