function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Thijs FaberThijs Faber 

Apex trigger - unintended currency modulation

Hi all, 

So my first trigger seemed to work in my sandbox. In my organisation we have multiple currencies:
1. EUR for group reporting (corporate currency)
2. ZAR at our first local entity (more foreign entities will added later).

When I used the trigger (see below) it rolls up the amounts but converts the ZAR amount into the EUR amount unintended (but does use the AR prefix). Simply multiplying by the conversion rate wuld lead to differences due to rounding. Is there an elegant solution for this?

Thanks in advance!
 
trigger TotalAmountTrigger on Account (after insert, after update, after delete, after undelete)
{
    Set<Id> accountIds = new Set<Id>();
    for(Account c: Trigger.isDelete? Trigger.old : Trigger.new){
        if(c.GroupName__c  != null)                                                     
        {
            accountIds.add(c.GroupName__c );
        }
    }  
    List<CustomerGroups__c> accList = new List<CustomerGroups__c>();
    for(AggregateResult currentAggResult:[SELECT GroupName__c  accId, SUM(Customer_Loan_Amount__c ) sumAmt FROM Account  WHERE GroupName__c  in:accountIds GROUP BY GroupName__c ])
    {
        CustomerGroups__c acc = new CustomerGroups__c();
        acc.Id = (Id)currentAggResult.get('accId');
        acc.Customer_Group_OLB__c = (decimal)currentAggResult.get('sumAmt');
        accList.add(acc);
        accountIds.remove((Id)currentAggResult.get('accId'));
    }
   
    for(Id currAccId : accountIds)
    {
        CustomerGroups__c acc = new CustomerGroups__c();
        acc.Id = currAccId;
        acc.Customer_Group_OLB__c = null;
        accList.add(acc);
    }
    update accList;
}

Kind regards,
Thijs
 
Best Answer chosen by Thijs Faber
Maharajan CMaharajan C
Hi Thijs Faber,

When we use the Aggregate function with SUM it will automatically convert the Amounts to Corporate Currency - or organization default currency.

Reference Link: https://developer.salesforce.com/forums/?id=906F00000008omIIAQ

If it doesn't work in the Report you can easily convert the currencies in Amount field.

Hope this will helps to you!!!

If it helps choose this as a best answer!!!

Thanks,
Raj

All Answers

Maharajan CMaharajan C
Hi Thijs Faber,

When we use the Aggregate function with SUM it will automatically convert the Amounts to Corporate Currency - or organization default currency.

Reference Link: https://developer.salesforce.com/forums/?id=906F00000008omIIAQ

If it doesn't work in the Report you can easily convert the currencies in Amount field.

Hope this will helps to you!!!

If it helps choose this as a best answer!!!

Thanks,
Raj
This was selected as the best answer
Thijs FaberThijs Faber
Ok. Thanks, I adjusted decimal places to mitigate for the rounding effects.