Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Sum or add two currency field values

charanpreet
Kilo Expert

I am trying to sum up two currency field values with below code where cur1 and cur 2 are the values of currency field.

var val1 = cur1 + cur2;

the ouput is 200.001000.00, where cur1 = 200.00 and cur2=1000.00

The output should be 1200.00 and to be set in the currency field itself.

5 REPLIES 5

prithvirajchaud
Mega Expert

Hi,



Are you trying to do var result   = cur1 + cur2; ?


If yes then change it to var result   = +cur1 + +cur2;


i'm pretty sure this should work. The '+' is for adding the values as numbers rather than strings



Thanks



P.S. like this answer , mark as helpful or correct if you feel so.


This works - thanks

This saved my work day! Thanks a lot!

I'd like to share that it also works in this case of coding, using getAggregate('SUM', 'u_column_name') function.

// EXAMPLE OF CODE

var gr = new GlideAggregate('u_table_name');
gr.addAggregate('SUM', 'u_currency_column');
gr.query();

if(gr.next()) {
    var cost = gr.getAggregate('SUM', 'u_currency_column');

    var total = +total + +cost;
    
    gs.info('TOTAL COSTS: ' + total + '\n');
}
​

 

In my case the answer was correctly sum up:

TABLE INFORMATION

*** Script: SEGMENTO: example1
*** Script: QTD TICKETS: 2
*** Script: TOTAL COST: 342.6800
*** Script: SEGMENTO: example2
*** Script: QTD TICKETS: 3
*** Script: TOTAL COST: 216023.1800
SUM UP INFORMATION
*** Script: QTD TICKETS2: 5
*** Script: TOTAL COSTS2: 216365.86


Appreciate the ServiceNow Community! 😄

 

Deepa Srivastav
Kilo Sage

Hi,



Try below:



var x=parseInt(cur1)+parseInt(cur2);



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.