Need to help on scripting

Khalid9030
Tera Contributor

Hi Team,

 

I have written below script in background script the purpose of the script is Group incidents by assigned to and list only grouped records where the assigned to have more than one record. The error i am getting is 

invalid return

 

var agg = new GlideRecord('incident');

agg.addAggregate('COUNT','assigned_to');

agg.query();

var arr =[];

while(agg.next()){

 

          var incidentCount = agg.getAggregate('COUNT','assigned_to');

 

          if(incidentCount >1) {

 

                   gs.info('Display  the count',[incidentCount])

         

                   var inc = new GlideRecord('incident');

                   inc.addQuery('assigned_to'+agg.assigned_to);

                   inc.query();

                   while(inc.next()){

 

                             arr.push(inc.sys_id.toString())

                   }

 

          }

     return arr;

          gs.print([incidentCount]);

}

1 ACCEPTED SOLUTION

swathisarang98
Giga Sage
Giga Sage

@Khalid9030 ,

 

The code which you have written will not satisfy your requirement, i have modified the entire code ,Please refer the working code as below,

 

 

 

var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT', 'assigned_to');
agg.orderBy('assigned_to');
agg.query();
var incidentCount = 0;
while (agg.next()) {
    var num = [];
    
    incidentCount = agg.getAggregate('COUNT', 'assigned_to');

    if (incidentCount > 1) {

        var gr = new GlideRecord('incident');
        gr.addQuery('assigned_to', agg.assigned_to);
        gr.query();
        while (gr.next()) {
            num.push(gr.getValue('number'));
        }

        
        gs.info('count for ' + agg.assigned_to.name + ':' + incidentCount + " Incident Numbers : " + num );


    }

}

 

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

View solution in original post

5 REPLIES 5

swathisarang98
Giga Sage
Giga Sage

@Khalid9030 ,

 

The code which you have written will not satisfy your requirement, i have modified the entire code ,Please refer the working code as below,

 

 

 

var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT', 'assigned_to');
agg.orderBy('assigned_to');
agg.query();
var incidentCount = 0;
while (agg.next()) {
    var num = [];
    
    incidentCount = agg.getAggregate('COUNT', 'assigned_to');

    if (incidentCount > 1) {

        var gr = new GlideRecord('incident');
        gr.addQuery('assigned_to', agg.assigned_to);
        gr.query();
        while (gr.next()) {
            num.push(gr.getValue('number'));
        }

        
        gs.info('count for ' + agg.assigned_to.name + ':' + incidentCount + " Incident Numbers : " + num );


    }

}

 

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang