0

I am working on a OptyLineItem trigger that does a rollup summary on the Opty header of a custom currency field on the OptyLineItem. The default currency is USD and Advanced Multi-currency is enabled. The code below works find for Optys in USD, but for other currencies I am running into an issue. It seems that the rollup value is being inserted into the DB already converted, but is being treated by Salesforce as USD. For example, if the summary of the line items is 100 GBP and the exchange rate is 1 GBP = .665114 USD, then the summary field looks like this: GBP 150.35 (USD 226.05). My code is below. My understanding from this page is that the Sum Aggregate query should automatically convert the currency to the default, but I am obviously missing something. Thanks in advance for your help.

public class BaselineRollupSummary {

    public static void baselineSummaryCalculation(List<OpportunityLineItem> optyLineItems){
        Set<ID> optyIDSet = new Set<ID>(); //set of affected Optys
        Map<ID, Opportunity> optysForUpdate = new Map<ID, Opportunity>();
        List<AggregateResult> optysWithBaselineSummary = new List<AggregateResult>();
        List<Opportunity> optysToBeZeroed = new List<Opportunity>();

        //build Set of Optys
        for(OpportunityLineItem oli : optyLineItems){
            optyIDSet.add(oli.OpportunityId);
        }

        System.debug('Opty Set Size: '+OptyIDSet.size());

        optysWithBaselineSummary = [SELECT OpportunityId, SUM(Baseline_Amount__c) FROM OpportunityLineItem WHERE OpportunityId IN :optyIDSet GROUP BY OpportunityId];

        if (optysWithBaselineSummary.size() < optyIDSet.size()){
            optysToBeZeroed = [SELECT Id, Baseline_Amount_Temp__c FROM Opportunity WHERE Id IN :optyIDSet];
            System.debug('optysToBeZeroed Size: '+optysToBeZeroed.size());

            for(Opportunity opty : optysToBeZeroed){

                opty.Baseline_Amount_Temp__c = 0;

            }

            update optysToBeZeroed;

        }

        System.debug('Aggregate Result Size: '+optysWithBaselineSummary.size());

        for(AggregateResult ar : optysWithBaselineSummary){
            Opportunity optyForUpdate = New Opportunity (Id = (Id)ar.get('OpportunityID'));
            optyForUpdate.Baseline_Amount_Temp__c = (Decimal)ar.get('expr0');
            optysForUpdate.put(optyForUpdate.Id, optyForUpdate);
        }

        update optysForUpdate.values();

    }  //end of baselineSummaryCalculation method

}  //end of BaselineRollupSummary class 
  • what is the value of Opportunity.currencyIsoCode for the Oppo in question with GBP OLI? – cropredy Mar 17 '15 at 21:55
  • It is also GBP. – Mike Lankfer Mar 18 '15 at 11:37
  • Ok, so I think I figured out my issue. The Sum value is being inserted in USD instead of in the Opty Currency. I assume that means I have to convert the values before I update the header. Can anyone point me to instructions to do that in an advanced multi-currency org? – Mike Lankfer Mar 18 '15 at 14:21

Your Answer

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

Browse other questions tagged or ask your own question.