+ Start a Discussion
Arundhati DebArundhati Deb 

hi, i want to write a trigger to update opportunity currency same as the opportunity owner currency in before insert.

Hi

I have a scenario where the account currency and opportunity currency can be different. The opportunity currency should be the same as opportunity owner currency. I have to write a trigger for it. Can anyone help me with it.
Highly appreciated.
Best Answer chosen by Arundhati Deb
Abdul KhatriAbdul Khatri
@Arundhati

I see some issues with your code so here is your code with some corrections.
 
public static void Opportunitycurrency (List<Opportunity> newOptyList) {
        
        List<Id> oppOwnerIds = new List<Id>();
        for(Opportunity opp: newOptyList)
        {
            if(opp.OwnerId != null)
            {  
                oppOwnerIds.add(opp.OwnerId); 
            }
        }
        
        Map<Id, User> userList = new Map<Id, User>([SELECT Id, Name, CurrencyIsoCode FROM User WHERE Id IN :oppOwnerIds]);
        
        for(Opportunity opp: newOptyList)
        {
            if(opp.type != 'Renewal')
            {
                opp.currencyisocode=userList.get(opp.OwnerId).currencyisocode;
            }
        }
    }

 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Arundhati,

Please find the sample trigger below for the same and tweak it as per your need.
trigger currencyTransfer on Opportunity (before insert) {
  for(Opportunity opp : Trigger.new){<br>   Account myaccount=[select CurrencyIsoCode from Account where id=:opp.AccountId];
   opp.CurrencyIsoCode = myaccount.CurrencyIsoCode;
   }
}<br><br>It won't deal with mass update SOQL Limit
Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

 
Arundhati DebArundhati Deb
Hi, I want a trigger where opportunity currency is same as opportunity owner currency. And opportunity owner and account owner are different.
Arundhati DebArundhati Deb
I have written the following code in trigger handler:

public static void Opportunitycurrency (List<Opportunity> newOptyList) {
        Set<Id> oppOwnerIds = new Set<Id>();
        for(Opportunity opp: newOptyList)
        {
          if(opp.OwnerId != null)
         {  
          oppOwnerIds.add(opp.OwnerId); 
        }
        }
         List<User> ownerList = ([SELECT Id, Name, CurrencyIsoCode FROM User WHERE Id IN               :oppOwnerIds]);  
        Map<Id, User> userById=new Map<Id, User>();
        userById.putall(ownerList);
        for(Opportunity opp: newOptyList)
        {
            if(opp.type != 'Renewal'){
                oppOwnerIds.add(opp.OwnerId);
                opp.currencyisocode=userById.get(opp.OwnerId).currencyisocode;
            }
        }
    }
Abdul KhatriAbdul Khatri
Here is the trigger
trigger UpdateOpportunityCurrency on Opportunity (before insert) {
    
    for(Opportunity opp : Trigger.new) {
        opp.CurrencyIsoCode = opp.Owner.CurrencyIsoCode;
    }

}

 
vijayabhaskarareddyvijayabhaskarareddy
HI @ Arundhati,

Try below code,

Opportunity owner available only when opportunity created. Before Inserting an opportunity, it doesn't have any owner. So we have to take current logged in user as opportunity ower. 
 
trigger CurrencyTransfer on Opportunity (before insert) {

  User Usr = [SELECT User_currency__C, Id FROM User WHERE Id = : UserInfo.getUserId()];

for(Opportunity opp : Trigger.new) {
       opp.Oppo_Currency__c = Usr.User_Currency__c;
        
      }
}



Kindly let us know if it helps you.
Regards,
Vijay

 
Abdul KhatriAbdul Khatri
@Vijay

That is not true statement that Opportunity Owner is not available, please confirm and rever back with your confirmation. It is available and my code is working fine. I intentionally didn't use Current User base as it may fail if moving forward same code need to be used for the update where there is a change that the Opportunity Owner is not the logged in user. 
 
Arundhati DebArundhati Deb
Opportunity owner is available.
This trigger is to be used while creating opportunity
Abdul KhatriAbdul Khatri
@Arundhati

I see some issues with your code so here is your code with some corrections.
 
public static void Opportunitycurrency (List<Opportunity> newOptyList) {
        
        List<Id> oppOwnerIds = new List<Id>();
        for(Opportunity opp: newOptyList)
        {
            if(opp.OwnerId != null)
            {  
                oppOwnerIds.add(opp.OwnerId); 
            }
        }
        
        Map<Id, User> userList = new Map<Id, User>([SELECT Id, Name, CurrencyIsoCode FROM User WHERE Id IN :oppOwnerIds]);
        
        for(Opportunity opp: newOptyList)
        {
            if(opp.type != 'Renewal')
            {
                opp.currencyisocode=userList.get(opp.OwnerId).currencyisocode;
            }
        }
    }

 
This was selected as the best answer
Abdul KhatriAbdul Khatri
@Arundhati Was that helpful.?
Arundhati DebArundhati Deb
@Abdul Khatri thanks, it worked.
emma ferrisemma ferris
I admire San Diego. it somewhat is 2d to my abode city, Kailua. at present, I stay in
https://www.customassignment.com/ a community outdoors in Augsburg, Germany. this isn't any longer San Diego and it truthfully isn't Kailua, even though it somewhat is between some rolling hills and a selection of woodland and eco-friendly fields. it somewhat is positive
Andrew GuptaAndrew Gupta
Arisen with the same question but the trigger corrected helped me to get it update before insert. thanks