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
Aravind RAravind R 

Comparing currency field with a dollor value in an if statement

Hi,

I have a currency field on opportunity calles Currency1__c which can be any currency type(Multi-currency organization).

I want to compare this field with 700USD value
if(opptyrecord.Currency1__c > 700USD){

}

If Currency1__c is coming in INR, it should convert and compare with dollor value.

Please help in getting solution for this.

Thanks in advance
Best Answer chosen by Aravind R
Ezdhan Hussain S KEzdhan Hussain S K
HI Aravind,

You need to perform conversion before doing any operation in your code. Achieve this by using currencyType sobject in your ORG.
Exchange rates are stored in CurrencyType Sobject. Refer below link for info on currencyType

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_currencytype.htm (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_currencytype.htm" target="_blank)

For every multi currency ORG there will be a corporate currency. All exchange rates are calculated against this corporate currency.
So now let's assume USD is your corporate currency. 

Query conversion rates and put it in a map
map<string,decimal> queriedcurrencies = new map<string,decimal>();
for(CurrencyType c : SELECT ISOCode, ConversionRate FROM CurrencyType WHERE IsActive=TRUE]){
	queriedcurrencies.put(c.ISOCode,c.ConversionRate);
}
get current objects ISO code
object__c obj : [select id,currency__c from object__c];
string currencycode = obj.get('CurrencyIsoCode');
decimal convertedvalue = 0;
  for(STRING d : queriedcurrencies.KEYSET()){
    if(currencycode == d){
      conversionrate = queriedcurrencies.get(d);
      convertedvalue = obj.currency__C / conversionrate ;
    }
  }
if(convertedvalue > 700 ){
 /// do your logic
}

If that helps mark this as Best answer. It will help others.
 

All Answers

Ashish DevAshish Dev
If currently every loggedin user's default  currency is fixed ie. USD then convertCurrency(amount) function will return USD.
Aravind RAravind R
Hi Ashish,

Thank you for your valuable time. I need to use this logic in apex code , not in any formula. And also value can come in any currency type.
Ashish DevAshish Dev
try to use this function in soql query like this ..
account acc = [select id, convertCurrency(AnnualRevenue) from account where id = '00190000014Ljyq'];
system.debug(acc.AnnualRevenue);
Ezdhan Hussain S KEzdhan Hussain S K
HI Aravind,

You need to perform conversion before doing any operation in your code. Achieve this by using currencyType sobject in your ORG.
Exchange rates are stored in CurrencyType Sobject. Refer below link for info on currencyType

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_currencytype.htm (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_currencytype.htm" target="_blank)

For every multi currency ORG there will be a corporate currency. All exchange rates are calculated against this corporate currency.
So now let's assume USD is your corporate currency. 

Query conversion rates and put it in a map
map<string,decimal> queriedcurrencies = new map<string,decimal>();
for(CurrencyType c : SELECT ISOCode, ConversionRate FROM CurrencyType WHERE IsActive=TRUE]){
	queriedcurrencies.put(c.ISOCode,c.ConversionRate);
}
get current objects ISO code
object__c obj : [select id,currency__c from object__c];
string currencycode = obj.get('CurrencyIsoCode');
decimal convertedvalue = 0;
  for(STRING d : queriedcurrencies.KEYSET()){
    if(currencycode == d){
      conversionrate = queriedcurrencies.get(d);
      convertedvalue = obj.currency__C / conversionrate ;
    }
  }
if(convertedvalue > 700 ){
 /// do your logic
}

If that helps mark this as Best answer. It will help others.
 
This was selected as the best answer
Aravind RAravind R
Thanks Ezdhan. It really helped me a lot. Cheers :)