SUM with 2 currency values

xif1
Mega Expert

Hi everyone,

 

I've   a little problem with addition in service now.

 

below my script:

function test()
{
   var count = new GlideAggregate('fm_expense_line');
   count.addEncodedQuery('source_id=' + current.sys_id);
   count.addAggregate('AVG', 'amount');
   count.query();     
   while (count.next()) {   
       var sum = count.getAggregate('AVG', 'amount');
       gs.log(" Update counts: SUM = " + sum );
   }
}

 

I've a table call 'fm_expense_line' with currency value, like $1000 and $50. I try to do an addition with that for have a $1500 result.

Actually, this script juste print in output the two retrieved values :

 

-> Update counts: SUM = 1000.00

-> Update counts: SUM = 50.00

 

How I can make an addition with that ? 'cause each iteration 'var sum' is flush for the new value...

Thank's for your help.

1 ACCEPTED SOLUTION

Hi Harikrishnan,



Thank's for your quick answer.


The output stay the same with :



-> Update counts: SUM = 1000.00


-> Update counts: SUM = 50.00



I think the problem is not the query. I just don't understand with it return the two values not the sum of them...



EDIT I've found an issue below :






function doSum()    


      {    


  var j = 0;


  var tarray = new Array();


  tarray[j] = new Array();


  var ga = new GlideAggregate('fm_expense_line');    


  ga.addQuery('source_id',current.sys_id);    


  ga.addAggregate('SUM', 'amount');    


  ga.query();        


     


  while (ga.next())


  {        


              var sum = ga.getAggregate('SUM', 'amount');


            tarray[j] = sum;


            if (j != 0)


              {


                        var calc = parseFloat(tarray[j - 1].toString());


                        var sum2 = calc + parseFloat(tarray[j].toString());


              }


            j++;


          }


  return (sum2);


}


View solution in original post

3 REPLIES 3

xif1
Mega Expert

* It's SUM, not AVG for addAggregate and get Aggregate


harikrish_v
Mega Guru

Hi Romain,



Can you try this:-



function test()


      {


          var ga = new GlideAggregate('fm_expense_line');


          ga.addQuery('source_id',current.sys_id);


          ga.addAggregate('SUM', 'amount');


          ga.query();    


          while (ga.next()) {    


              var sum = ga.getAggregate('SUM', 'amount');


              gs.log(" Update counts: SUM = " + sum );


          }


      }


Thanks & Regards,


Hari


Hi Harikrishnan,



Thank's for your quick answer.


The output stay the same with :



-> Update counts: SUM = 1000.00


-> Update counts: SUM = 50.00



I think the problem is not the query. I just don't understand with it return the two values not the sum of them...



EDIT I've found an issue below :






function doSum()    


      {    


  var j = 0;


  var tarray = new Array();


  tarray[j] = new Array();


  var ga = new GlideAggregate('fm_expense_line');    


  ga.addQuery('source_id',current.sys_id);    


  ga.addAggregate('SUM', 'amount');    


  ga.query();        


     


  while (ga.next())


  {        


              var sum = ga.getAggregate('SUM', 'amount');


            tarray[j] = sum;


            if (j != 0)


              {


                        var calc = parseFloat(tarray[j - 1].toString());


                        var sum2 = calc + parseFloat(tarray[j].toString());


              }


            j++;


          }


  return (sum2);


}