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

Hi @Khalid9030 ,

 

There were few errors in the code i have corrected it please do give it a try ,

 

 

var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT', 'assigned_to');
agg.query();
var arr = [];
var num = [];
if(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());
            num.push(inc.getValue('number'));
        }

    }
    gs.print('array : ' + arr);
    gs.print('Number : ' + num);

}

 

 

 

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

Regards,

Swathi Sarang

dgarad
Giga Sage

Hi @Khalid9030 

 try the

var incidentCount =0;
var arr=[];
var assginto=[];
var agg = new GlideAggregate('incident');

agg.addAggregate('COUNT','assigned_to');
agg.query();
while(agg.next()){

incidentCount = agg.getAggregate('COUNT','assigned_to');
assginto.push(agg.assigned_to.toString());


 }
 gs.print('Display the count'+incidentCount);


		if (incidentCount > 1) {



    

    var inc = new GlideRecord('incident');

    inc.addQuery('assigned_to' , assginto);

    inc.query();

    while (inc.next()) {



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

    }
	gs.print(arr);
    return arr;

          

below code.

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Sohail Khilji
Kilo Patron
Kilo Patron

@swathisarang98  yes you own the right , credits to you... 🙂

 

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hello @Sohail Khilji ,

 

Can you please let me know what is the difference in my  below code and your code?

 

I can see only variable names and info texts are changed .And more over its not the working code yet as i re-tested it once again and was about to post the correct code

 

 

Regards

Swathi sarang