Is it possible to use GlideAggregate for SUM with a Currency type field?

JeremiP
Tera Guru

I cannot figure out the proper way to make a sum of the currency field found in the GlideAggregate query.

As per the below script, whether using a while loop or an if, it seems to go by each record one by one, returning the current record's field value, rather than summing it up.

What I'm trying to achieve is to calculate a sum of attached records' "total cost" Currency field.

updateInvoiceAmount: function(invoice) {
        var workOrders = new GlideAggregate('u_work_order');
            workOrders.addAggregate('SUM','u_total_cost');
            workOrders.addQuery('u_invoice_ref'invoice);
            workOrders.query();
        if(workOrders.next()){
            var totalCost = workOrders.getAggregate('SUM','u_total_cost');
            gs.info(totalCost);
        }
        var invoiceRecord = new GlideRecord('u_vendor_invoice');
        if (!invoiceRecord.get(invoice))
            return false;
        invoiceRecord.u_amount = totalCost;
        return invoiceRecord.update();
    },

What's the use of GlideAggregate if it wouldn't be meant for use with this type? I can see that the official docs suggest it's possible:
https://docs.servicenow.com/bundle/newyork-platform-administration/page/administer/currency/concept/currency-values-scripts.html
i.e.: "When you use GlideAggregate on currency or price fields, you are working with the reference currency value. "

2 REPLIES 2

Milind Gharte
Kilo Guru

Hi,

Here is link which has  example of GlideAggregate which will help you.

https://community.servicenow.com/community?id=community_question&sys_id=11fff6eddb58dbc01dcaf3231f96...

 

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

 

If it Helps,please mark it as Correct and Helpful.


Warm Regards,


Milind

jatinponnaluri
ServiceNow Employee
ServiceNow Employee

setGroup(Boolean b)

Sets whether to group the results.


In your code add this line
workOrders
.setGroup(false);


If it helps,please mark it as Correct and Helpful.

Thanks,
Jatin