0

Our System is multi currency enabled with Advanced currency management. My requirement is get the currency in USD(corporate currency) on a given Opportunity and stamp it to a custom field.

I have written Apex trigger on Opportunity object by using convertCurrency function knowing the fact that convertCurrency returns value in user local currency.

I thought since Apex trigger is running in system context it would take the DEFAULTCURRENCYISOCODE from the user object as "USD" on system context. Apparently that is not the case.

Is that possible that the following code will take "USD" as the DEFAULTCURRENCYISOCODE to return the corporate currency value?

    trigger CopyConvertedValue on Opportunity (after insert,after update )
{
    Set<Id> oppIds = new Set<Id>();
    //List<Opportunity> oppsToUpdate = new List<Opportunity>();
    Map<Id, Opportunity> oppsToUpdate = new Map<Id,Opportunity>();
    for(Opportunity Oppty: Trigger.New){
      if(Trigger.isInsert){      
        oppIds.add(Oppty.Id);
        System.debug('I am inside'+userinfo.getDefaultCurrency());
      }
      else if((Oppty.Amount_2__c != Trigger.oldMap.get(Oppty.Id).Amount_2__c ) || Trigger.oldMap.get(Oppty.Id).TriggerToSE__c == 'ConvertForMkto' )
      {
        oppIds.add(Oppty.Id);
        System.debug('Iam edit'+userInfo.getDefaultCurrency());
      }
    }
    if(!oppIds.isEmpty()){
      for (Opportunity opp : [SELECT Converted_Amount_2__c,convertCurrency(Amount_2__c) FROM Opportunity WHERE Id IN :oppIds]){
        oppsToUpdate.put((Id)opp.get('Id'),new Opportunity(Id=(Id)opp.get('Id'),Converted_Amount_2__c = (Double)opp.Amount_2__c,TriggerToSE__c=null));
      }
    }
    if(oppsToUpdate.size()>0){
      Database.update(oppsToUpdate.values(),false);
    }
}

For example when creating an Opportunity with currency code as "Korean Won" the debug statement is returning that particular user default currency code.

enter image description here

  • 1
    What determines where the currency code comes from? Is it based on the owner of the Opportunity? – battery.cord Dec 19 '17 at 14:50
  • Currency code is coming from Opportunity. – user28452 Dec 19 '17 at 15:20
0

I was able to resolve with the following approach.

    trigger CopyConvertedValue on Opportunity (before insert ,before update )
{

   Map<ID,DatedConversionRate> isoWithRateMap = new Map<ID,DatedConversionRate>([SELECT NextStartDate,StartDate,IsoCode,ConversionRate FROM DatedConversionRate]);

  if (checkRecursive.runOnce()){
    for(Opportunity opp: trigger.new){      

        Decimal convertedValue;
        for (DatedConversionRate dcr: isoWithRateMap.values()) {
          if (dcr.isoCode != opp.CurrencyIsoCode)                                   continue;   // look for a match in table against input currency
          if (opp.CloseDate < dcr.startDate || opp.CloseDate >= dcr.nextStartDate)  continue;
          convertedValue =  opp.Amount_2__c / dcr.conversionRate;
          break;
        }
        opp.Converted_Amount_2__c = convertedValue;    

      }
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.