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
Jack daniel 16Jack daniel 16 

Write a Trigger

write a trigger to update opportunity currency the same as the opportunity owner currency in before insert.
Best Answer chosen by Jack daniel 16
Deepali KulshresthaDeepali Kulshrestha
Hi Jack,

Greetings to you!

I have written the following code as per your requirement. Please try the below code.
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;
            }
        }
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Jack,

Greetings to you!

I have written the following code as per your requirement. Please try the below code.
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;
            }
        }
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Jack,

Use bellow trigger it may helpful for you
trigger Assignment2 on Opportunity (before insert) {
    if(trigger.isBefore && trigger.isInsert){
        Opportunitycurrency.currencyMethod(trigger.new);
    }

}

public class Opportunitycurrency {
    public static void currencyMethod (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;
            }
        }
    }
    
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com