+ Start a Discussion
Al SugarmanAl Sugarman 

Need help on a trigger converting currency to USD

I created a number field that I am trying to populate with the USD amount of a currency field on opportunities. I have tried two methods but still the field display only the currecy thet the opportunity is in...any help is appreciated...

Method 1
trigger convertcurrency on Opportunity (before insert, before update) {
    for(Opportunity o :trigger.new){
        Boolean isChanged = false;
        if(trigger.isUpdate && o.Reporting_Total__c != trigger.oldMap.get(o.Id).Reporting_Total__c){
            isChanged = true;
        {
        Opportunity[] opps = [select convertCurrency(Reporting_Total__c) from Opportunity where Id = :o.Id];
                        o.Reporting_Total_in_USD__c = o.Reporting_Total__c;
                
        }
        }
    }
}

===============================================
Method 2
trigger convertcurrency on Opportunity (before insert, before update) {
    for(Opportunity o :trigger.new){
        Boolean isChanged = false;
        if(trigger.isUpdate && o.Reporting_Total__c != trigger.oldMap.get(o.Id).Reporting_Total__c){
            isChanged = true;
        {
        
        For (AggregateResult[] opp : [select name, SUM(Reporting_Total__c) rt from Opportunity where id = : o.id  group by Name ])
        
        for(AggregateResult ar : opp){
        
                        o.Reporting_Total_in_USD__c = o.Reporting_Total__c;
                                      }
        }
    }
}
}
 
Rahul BorgaonkarRahul Borgaonkar
Hi,

You can use formula field or process builder(new tool) to achieve this.
Where are you geting conversion formula from ? I assume Reporting_Total__c don't have USD value.
Regards,

Rahul
 
Al SugarmanAl Sugarman
Hi Rahul,

I have tried a formual field, both as currency and number. I was unable to use the convertcurrency() function in either formula.but got the  just adding the convertcurrency() function retunred the Error: Incorrect parameter type for function 'convertcurrency()'. Expected Currency, received Number...even though this is a currecy field. 

I have also tried the following in a trigger

 if(currencies.get(o.CurrencyIsoCode).ConversionRate != null){
                o.Reporting_Total_in_USD__c = o.Reporting_Total__c / currencies.get
(o.CurrencyIsoCode).ConversionRate;

This gets me closer but if the close date on the opportunity is any other month than the current month the values do not match.

Thanks,
Al . 
Rahul BorgaonkarRahul Borgaonkar
Hi,

convertcurrency() is used when multicurrency is enabled. Is that enabled and set USD and local currency in your system?

Please check link below.

http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_querying_currency_fields.htm

Could you please share forumla field and formula you wrote to convert Your currency to USD?

Thanks,

Rahul
 
Luis Esquivel 2Luis Esquivel 2
@Al Sugarman, Have you been able to accomplish the desired results? This is something I've been attempting for some time now.

Thanks,

Luis
Paras JainParas Jain
Create Formual field ---   1 / CURRENCYRATE(TEXT(CurrencyIsoCode)) * Support_Entitlement_Value__c