Print Assignment_group and its assigned incident numbers

Avinash5410
Tera Contributor

I want to print assignment group : incident numbers......... like Hardware : INC000111,INC0000233 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Avinash5410 Here is the script which can get you this data.

var glideIncident = new GlideAggregate('incident');
glideIncident.groupBy('assignment_group');
glideIncident.addAggregate('COUNT');
glideIncident.query();
while(glideIncident.next()){
    var assignment_group ={'id':glideIncident.getValue('assignment_group'),'name':glideIncident.getDisplayValue('assignment_group')};
    if(assignment_group.id!=''){
        var glideIncidentRec = new GlideRecord('incident');
        glideIncidentRec.addQuery('assignment_group',assignment_group.id);
        glideIncidentRec.query();
        var incidentNum ='';
        while(glideIncidentRec.next()){            
            incidentNum = incidentNum+glideIncidentRec.getValue('number')+',';
        }
        gs.print(assignment_group.name+':'+incidentNum);
    }
    
}

Here is the output.

Screenshot 2023-10-28 at 12.52.32 AM.png

Hope this helps.

View solution in original post

8 REPLIES 8

Jaspal Singh
Mega Patron
Mega Patron

Hi Avinash,

In case, you wish to further explore and learn. Try below giving link a check

Harish Bainsla
Kilo Patron
Kilo Patron

// Create a GlideAggregate query on the incident table
var incidentGR = new GlideAggregate('incident');

// Group the records by assignment group
incidentGR.addAggregate('COUNT', 'number');
incidentGR.groupBy('assignment_group');

// Execute the query
incidentGR.query();

// Initialize a variable to store the results
var result = '';

// Loop through the groups and their counts
while (incidentGR.next()) {
var assignmentGroup = incidentGR.getDisplayValue('assignment_group');
var incidentCount = incidentGR.getAggregate('COUNT', 'number');

// Append the assignment group and incident numbers to the result string
result += assignmentGroup + ': ';

// Query the incident numbers for the current assignment group
var incidentNumbers = new GlideRecord('incident');
incidentNumbers.addQuery('assignment_group', incidentGR.assignment_group);
incidentNumbers.query();

// Loop through the incident records and append their numbers to the result
while (incidentNumbers.next()) {
result += incidentNumbers.number + ',';
}

// Remove the trailing comma
result = result.slice(0, -1);

// Add a line break
result += '\n';
}

// Log or display the result
gs.info(result);

HarishBainsla_0-1698442407267.png

 

could you pls tell, how to get the output in json fotmat 

var ias= new GlideAggregate("incident");
ias.groupBy("assignment_group");
ias.addAggregate("COUNT");
//ias.addNullQuery("assignment_group");
ias.query();
while (ias.next()){
    var asgrpid = ias.getValue("assignment_group");
    var asgrpname = ias.getDisplayValue("assignment_group");
    if (asgrpid!=''){
var incgr = new GlideRecord("incident");
incgr.addQuery("assignment_group",asgrpid);
incgr.query();
while (incgr.next()){

incNum = incgr.getValue("number") + ",";
}
gs.print("{" + asgrpname + "}" + " : " + "{" + incNum + "}" );
    }
    }
 
 
 
Screenshot (234).png

But printing only first number, what's wrong in the script