• Peter Ormon
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Can anyone see why this code in an apex class called by a before update trigger would not work correctly?  
The FX_Rate__c object has records current through today and 3 years back for both GBP and EUR.  Yet the Date__c value that it populates is randomly 2 years prior to the selected date. (e.g.objOpp.FX_Rate_Date__c selected is 2017-04-30 yet the Date__c that gets populated is 2015-06-05.)
Code sample below:
list<FX_Rate__c> fxRate = [Select Id, FX_Rate__c,Currency_Code__c, Date__c from FX_Rate__c WHERE Currency_Code__c IN('GBP','EUR') ORDER BY Date__c DESC NULLS LAST];
            for (Opportunity objOpp :oppList)
            {
              if (objOpp.Ticket_Currency__c != 'USD')
             {
                        Date fxDate = objOpp.FX_Rate_Date__c;
                       if (fxRate.size() > 0)
                        {
                            for (FX_Rate__c rate :fxRate)
                            {                                
                                if(fxDate.isSameDay(rate.Date__c)  && objOpp.Ticket_Currency__c  == rate.Currency_Code__c)
                                {
                                    objOpp.FX_Rate__c = rate.Id;
                                    objOpp.FX_Rate_Date__c = fxDate;                                                                     
                                       break;                                       
                                }
                                fxDate = fxDate.addDays(-1);
                            }
                        }
Can anyone see why this code in an apex class called by a before update trigger would not work correctly?  
The FX_Rate__c object has records current through today and 3 years back for both GBP and EUR.  Yet the Date__c value that it populates is randomly 2 years prior to the selected date. (e.g.objOpp.FX_Rate_Date__c selected is 2017-04-30 yet the Date__c that gets populated is 2015-06-05.)
Code sample below:
list<FX_Rate__c> fxRate = [Select Id, FX_Rate__c,Currency_Code__c, Date__c from FX_Rate__c WHERE Currency_Code__c IN('GBP','EUR') ORDER BY Date__c DESC NULLS LAST];
            for (Opportunity objOpp :oppList)
            {
              if (objOpp.Ticket_Currency__c != 'USD')
             {
                        Date fxDate = objOpp.FX_Rate_Date__c;
                       if (fxRate.size() > 0)
                        {
                            for (FX_Rate__c rate :fxRate)
                            {                                
                                if(fxDate.isSameDay(rate.Date__c)  && objOpp.Ticket_Currency__c  == rate.Currency_Code__c)
                                {
                                    objOpp.FX_Rate__c = rate.Id;
                                    objOpp.FX_Rate_Date__c = fxDate;                                                                     
                                       break;                                       
                                }
                                fxDate = fxDate.addDays(-1);
                            }
                        }