0

I have an Apex Trigger that will Rollup Summary my Opportunity__c object's three fields: InvoiceTotal__c, InvoiceGrossProfit__c, and InvoiceDiscountAmount__c. The trigger will fire every time an Invoice__c record is updated. I'm able to query and aggregate my Invoice's fields just fine but is not being incorporated correctly with our Multi-Currency.

So, I've added convertCurrency() as shown below.

if(!oppIdSet.isEmpty()){
    for(AggregateResult res : [SELECT OpportunityId__c, SUM(convertCurrency(InvoiceTotal__c))amt1, 
                                                        SUM(convertCurrency(GrossProfit__c))amt2, 
                                                        SUM(convertcurrency(DiscountAmount__c))amt3 
                               FROM Invoice__c  
                               WHERE OpportunityId__c IN :oppIdSet 
                               GROUP BY OpportunityId__c]) {
        oppListToUpdate.add(new Opportunity__c(Id=(Id)res.get('OpportunityId__c'),
                                               InvoiceTotal__c=(Decimal)res.get('amt1'),
                                               InvoiceGrossProfit__c=(Decimal)res.get('amt2'),
                                               InvoiceDiscountAmount__c=(Decimal)res.get('amt3')));
     }
}

if(!oppListToUpdate.isEmpty()){
    try{
        update oppListToUpdate;
    }catch(DmlException de){
        System.debug(de);
    }
}

Since enabling Multi-Currency, I have combined using SUM and convertCurrency() on my fields. The code is being compiled successfully and no errors are being shown in the Dev Console but when updating records, it is showing this error message (Line 42 is the "res" soql query row):

enter image description here

I'm not familiar with the error at all and have tried adding the queried fields to the GROUP BY but will then show a different error saying those fields cannot be grouped.

Am I missing something? What am I doing wrong exactly?

Thanks for your help.

1
  • FYI, if(!oppListToUpdate.isEmpty()){ is not necessary, it is okay to DML on an empty list.
    – sfdcfox
    May 1 '20 at 12:32
0

You're not allowed to use convertCurrency with aggregate functions:

You can’t convert the result of an aggregate function into the user’s currency by calling the convertCurrency() function. If a query includes a GROUP BY or HAVING clause, currency data returned by using an aggregate function, such as SUM() or MAX(), is in the org’s default currency.

The ORA error indicates an unhandled error at the database level, you should report this as a bug to Salesforce Technical Support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.