How to get currency code & value from Glide Aggregate SUM results?

DG12
Kilo Contributor

I have a cost field on a table that contains different currencies and values. I'm able to aggregate sum them, but I'm unsure how to display the results with the proper currency symbol and decimals.

var gr = new GlideAggregate('table_name');
gr.addAggregate('SUM','cost');
gr.setGroup(false);
gr.query();
gr.next();
gs.print(gr.getAggregate('SUM','cost'));

This sums up the values nicely, considering the different exchange rates, but the result is a string like this 1234.5678. The data type is not a problem, and I don't want to hard code a $ sign for USD, but instead let the system display the proper currency value based on who's looking at the results which results should be something like this instead: $1234.57 similar to how getDisplayValue() works when used directly on a currency field (such as the cost field in this case). Any help on how to produce this result with the resulting sum aggregate in this case would be appreciated.

3 REPLIES 3

sachin_namjoshi
Kilo Patron
Kilo Patron

Have you tried setting the correct Country code on the User's profile?  IT has an effect in determining the currency they see: 

 

https://docs.servicenow.com/bundle/jakarta-platform-administration/page/administer/currency/concept/session-and-reference-currency.html

 

Regards,

Sachin

DG12
Kilo Contributor

The user profiles each have a default country set up. For instance, when I use getDisplayValue() on the cost field, I get the expected $1234.57. The field contains the country code along with the value. and some are in GBP and others are in USD (which can be seen on the list view of the table). When I sum them up using SUM aggregate using my test profile, the correct values are added up in USD as I would expect.

However, when I use getAggregate to retrieve the value, I only get a string, which I believe is also the intended result, but is there a way to get an actual display value from the SUM aggregate that would automatically include the resulting currency symbol?

Stuart D
Kilo Contributor

Hi DG,

Did you get this sorted?  I have the same issue - want to get the aggregate result presented in correct monetary format.

Cheers,

Stu Diener