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
arcticcloudarcticcloud 

Opportunity Trigger, multiple currency

Hi all.


I am trying to fire a trigger when a new opportunity is created to select the account's default currency for this opportunity. 

 

trigger currencyTransfer on Opportunity (before insert) {
  for(Opportunity opp : Trigger.new){
   opp.CurrencyIsoCode = opp.Account.CurrencyIsoCode;
   }
}

 I guess it doesn't work to lookup the currency through the account this way - what else can I do? Any help would be greatly appreciated

goabhigogoabhigo

Try not putting any value in Opportunity currency. Since it is child of Account, automatically it will copy Account's currency. NOT 100% sure about this. If this doesn't work, then you will have query for that Account and in the select statement you have to include the currency field. Then use it..

arcticcloudarcticcloud

Unfortunately the opportunity currency is not dependant on the account currency - the default corprate currency rules this. I work as a business analyst so I am not sure how to get started on the account select statement, can anyone get me started?

Thanks in advance.

alex_from_75015alex_from_75015
trigger currencyTransfer on Opportunity (before insert) {
  for(Opportunity opp : Trigger.new){
Account myaccount=[select CurrencyIsoCode from Account where id=:opp.AccountId]; opp.CurrencyIsoCode = myaccount.CurrencyIsoCode; } }

It won't deal with mass update SOQL Limit
Naidu PothiniNaidu Pothini
trigger currencyTransfer on Opportunity (before insert)
{
	List<Id> accIDs = new List<Id>();

	for(Opportunity Opp : Trigger.new)
	{
		accIds.add(Opp.AccountId);
	}

	Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Name, CurrencyIsoCode FROM Account WHERE Id IN :accIds]);

	for(Opportunity opp : Trigger.new)
	{
		opp.CurrencyIsoCode = accMap.get(opp.AccountId).CurrencyIsoCode;
	}
}

 I guess this might work for you.

alex_from_75015alex_from_75015
For sure this is the perfect code Alexandre