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
Shawn Reichner 29Shawn Reichner 29 

Currency conversion issue within Apex

Hello awesome Devs!

I have the following Apex class that is ran eaxch morning to perfrom an aggregate results to compile all child records that are associated with a certain opportunity and sum up thier currency values from a formula field called Total Bookings Amount.  Then the class will grab that sum and place the total summed amount into a custom field on the Opportunity called Current Value.  

The issue is if the child record is in GBP currrency the aggregate results class will treat it as USD as that is our ORG's company currency and the value is off by the conversion amount.  

So for more context:
Child Record A - Total Booking Amount = 10 USD
Chile Record B - Total Booking Amount = 14.55 USD
Total Summed amoutn for both Child Records = 24.55 USd - Converted to 18.00 GBP on record. 

When Class runs it takes the GBP total listed above converted back into USD and populates this amount on the Parent Opportunity on the Current Value field so it shows a total of 24.55 GBP (really it is the USD rate, but since the Opportunity is in GBP it shows as GBP although it is not correct), however the conversion does not happen when this Current Value is populated although the amount standard field is converted to the proper amount which shows 18.00 GBP as the Amount. 

Hope this makes sense, but ultimately I am tryign to have the class convert the summed amount before populating on the parent Opportunity if the currencyIsoCode is GBP. 

Any thoughts????

Clas Code - 
 
global class AMPCurrentAmountBatching Implements Schedulable {

    global void execute(SchedulableContext sc){
        AMPCurrentAmountBatching();
    }
    @future
    static public void AMPCurrentAmountBatching(){
        
       List<Opportunity> opps = new List<Opportunity>();       
        
        For(AggregateResult objar : [SELECT OpportunityId__c Oid, SUM(Total_Booking_Amount__c) Amt
                                    FROM Zuora__Subscription__c WHERE OpportunityId__c !=null AND LastModifiedDate = LAST_N_DAYS:60
                                    GROUP BY OpportunityId__c])
        {
        
        Decimal d = (Decimal)objar.get('Amt');
        
            Opportunity Opp = new Opportunity();
            Opp.Id = (Id)objar.get('Oid');
            Opp.Current_Value__c = (Decimal)objar.get('Amt');
            opps.add(Opp); 
        }
        
        If(opps.size()>0){
           update opps;
        }
        
        
    }
    
    
}

 
Onur Kaya 14Onur Kaya 14
global class AMPCurrentAmountBatching Implements Schedulable {

    global void execute(SchedulableContext sc){
        AMPCurrentAmountBatching();
    }
    @future
    static public void AMPCurrentAmountBatching(){
        
        /************* Calculate Currency Code vs Conversion Rate ***********************/
		List<CurrencyType> currencyList = [SELECT Id, IsoCode, ConversionRate FROM CurrencyType];
      
        Map<String,Decimal> IsoCode_cRate = new Map<String,Decimal>();
      
		for(CurrencyType ct: currencyList){
			IsoCode_cRate.put(ct.IsoCode,ct.ConversionRate);
		}
		
		/********** Collect the opp ID vs ISO Code **************************************/
		
		Map<Id,String> oppId_vs_ISOCode = newMap<Id,String>();
		
		List<Zuora__Subscription__c> zSubLst = [SELECT OpportunityId__c, ISOCode FROM Zuora__Subscription__c WHERE OpportunityId__c !=null AND LastModifiedDate = LAST_N_DAYS:60];
		
		for(Zuora__Subscription__c zs: zSubLst){
			oppId_vs_ISOCode.put(zs.OpportunityId__c, zs.ISOCode);
		}
		
		/*************************END *********************/
		
		List<Opportunity> opps = new List<Opportunity>();       
        
        For(AggregateResult objar : [SELECT OpportunityId__c Oid, SUM(Total_Booking_Amount__c) Amt
                                    FROM Zuora__Subscription__c WHERE OpportunityId__c !=null AND LastModifiedDate = LAST_N_DAYS:60
                                    GROUP BY OpportunityId__c])
        {
        
        Decimal d = (Decimal)objar.get('Amt');
        
            Opportunity Opp = new Opportunity();
            Opp.Id = (Id)objar.get('Oid');
            Opp.Current_Value__c = (Decimal)objar.get('Amt') * IsoCode_cRate.get(oppId_vs_ISOCode.get(opp.Id));   //Add this code here
            opps.add(Opp); 
        }
        
        If(opps.size()>0){
           update opps;
        }
        
        
    }
    
    
}