• Onur Kaya 14
  • 0 Points
  • Member since 2019

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1
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){
    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');
           update opps;