I want to print groupby assignmentgroup and with count,numbers as well

ganeshnerni
Tera Contributor

I want to print groupby assignmentgroup and with count,numbers as well

6 REPLIES 6

VIJAY YACHAM1
Tera Contributor

Hello @ganeshnerni,

 

// Create a GlideAggregate object for the 'incident' table

var ga = new GlideAggregate('incident');

 

// Group by the 'assignment_group' field

ga.groupBy('assignment_group');

 

// Add a count aggregate to get the number of incidents per group

ga.addAggregate('COUNT');

 

// Execute the query

ga.query();

 

// Loop through the results and print the assignment group and count

while (ga.next()) {

    var groupName = ga.getDisplayValue('assignment_group') || 'No Assignment Group'; // Handle null/empty groups

    var count = ga.getAggregate('COUNT');

    gs.print('Assignment Group: ' + groupName + ' | Count: ' + count);

}

 

 

Please accept my solution and mark as helpful if it works 

Thanks 

Vijay

Prataps135
Mega Sage

Hi @ganeshnerni,

var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT');
agg.groupBy('assignment_group');
agg.query();

while (agg.next()) {
    var groupName = agg.assignment_group.getDisplayValue(); // Get group name
    var count = agg.getAggregate('COUNT'); // Get count of records
    gs.info('Assignment Group: ' + groupName + ', Count: ' + count);

    // Fetch numbers for this assignment group
    var gr = new GlideRecord('incident');
    gr.addQuery('assignment_group', agg.assignment_group);
    gr.query();
    
    var numbers = [];
    while (gr.next()) {
        numbers.push(gr.number.toString());
    }
    gs.info('Numbers: ' + numbers.join(', '));
}

 

Thanks and Regards,
Pratap Singh Sisodia