Glide Aggregate SUM not summing

donaldbates
Kilo Contributor

This should be super simple, but I'm missing it. I'm merely trying to count the chats from a customer table. The field in question is set as an integer. In this first section of code I would typically use an if instead of a while, but using the while to show how it's grouping the results. The second section of code is just so you can see what the actual record contains. The answer should be 3 when I sum. I followed Count and Sum and Avg and Min and Max, Oh My! as my example along with the wiki.

<!--BEGIN Find number of agents are on chats-->

var sc = new GlideAggregate('u_chat_round_robin');

sc.addAggregate('SUM', 'u_currentchats');

sc.addQuery('u_chat_queue', 'f03232f96f794e00c5afbd5dbb3ee4ed');

sc.query();

sc.setGroup(false);

var nc = 0;

while (sc.next()) {

nc = sc.getAggregate('SUM', 'u_currentchats');

gs.print('Number of chats ' + nc);

}

<!--END Find number of agents are on chats-->

<!--BEGIN Find number of agents are on chats-->

var sc = new GlideRecord('u_chat_round_robin');

sc.addQuery('u_chat_queue', 'f03232f96f794e00c5afbd5dbb3ee4ed');

sc.query();

var numberofchats = 0;

while (sc.next()) {

gs.print(sc.u_currentchats);

}

<!--END Find number of agents are on chats-->

RESULTS

*** Script: Number of chats 0

*** Script: Number of chats 1

*** Script: Number of chats 2

*** Script: 0

*** Script: 2

*** Script: 0

*** Script: 1

*** Script: 0

*** Script: 0

*** Script: 0

*** Script: 0

*** Script: 0

*** Script: 0

*** Script: 0

1 ACCEPTED SOLUTION

Thanks Donald, i now understand what's you're trying to accomplish and I believe i was able to see what we have wrong .



You just need to include the sc.setGroup(false); before the sc.query();



var sc = new GlideAggregate('u_chat_round_robin');


sc.addAggregate('SUM', 'u_currentchats');


sc.addQuery('u_chat_queue', 'f03232f96f794e00c5afbd5dbb3ee4ed');


sc.setGroup(false);


sc.query();


var nc = 0;


while (sc.next()) {


  nc = sc.getAggregate('SUM', 'u_currentchats');


  gs.print('Number of chats ' + nc);


}


View solution in original post

5 REPLIES 5

bernyalvarado
Mega Sage

Hi Donald,



have you tried removing the line which says:


sc.setGroup(false);


Thanks,


Berny


Hi Benny,



Yes, I have tried without that line. Also with that line both true and false.




I don't like it because it doesn't work the way I thought it should, however adding the following does give me the expected results. I need to test more.


Inside the while loop,


nc += parseInt(sc.getAggregate('SUM', 'u_currentchats'));



replaces


nc = sc.getAggregate('SUM', 'u_currentchats');


Thanks Donald, i now understand what's you're trying to accomplish and I believe i was able to see what we have wrong .



You just need to include the sc.setGroup(false); before the sc.query();



var sc = new GlideAggregate('u_chat_round_robin');


sc.addAggregate('SUM', 'u_currentchats');


sc.addQuery('u_chat_queue', 'f03232f96f794e00c5afbd5dbb3ee4ed');


sc.setGroup(false);


sc.query();


var nc = 0;


while (sc.next()) {


  nc = sc.getAggregate('SUM', 'u_currentchats');


  gs.print('Number of chats ' + nc);


}


Just in case... I also did a test code on my own to sum the state values of incidents which short description is test. It works like a charm just like how the new script i sent you should work as well.



I hope this is helpful!



var grA = new GlideAggregate('incident');


grA.addAggregate ('SUM', 'state');


grA.addQuery('short_description', 'test');


grA.setGroup(false);


grA.query();


while (grA.next()){


  gs.print('If we sum the state values where short description is test then we will get the number ' + grA.getAggregate('SUM','state') );


}



Result:


*** Script: If we sum the state values where short description is test then we will get the number 4



Just to validate the sum, without setGroup in the code we can see that 4 is truly the sum of the values for each record aggregate for each state.




Result when using the same query without grA.setGroup(false)...  


*** Script: If we sum the state values of the State 1 New where short description is test then we will get the number 2


*** Script: If we sum the state values of the State 2 Active where short description is test then we will get the number 2



and 2 + 2 = 4



Thanks,


Berny