- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2014 02:46 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2014 04:19 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2014 02:48 AM
* It's SUM, not AVG for addAggregate and get Aggregate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2014 02:56 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2014 04:19 AM
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);
}