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
jishan royjishan roy 

how to create helper and handler in this trigger

I want to create helper and handler of this trigger:

trigger oppOwnerTrigger on Opportunity (before insert,before update) {
    set<id> setAccountId = new set<id>();
    for(Opportunity opp: trigger.new){
        if(opp.AccountId !=Null){
            setAccountId.add(opp.AccountId); 
        }
    }
    Map<id,Account> mappaccidwithvalues = new map<id,Account>();
    
    for(Account acc:[select id,ownerid from Account where id=:setAccountId]){
        mappaccidwithvalues.put(acc.id,acc);
    }
    for (Opportunity op:trigger.new){
        if(mappaccidwithvalues.containskey(op.accountid)){
            op.ownerId = mappaccidwithvalues.get(op.accountid).ownerid;
        }
    }
}
Best Answer chosen by jishan roy
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jishan,

The trigger will be as below.
 
trigger oppOwnerTrigger on Opportunity (before insert,before update) {
    oppOwnerTriggerHandler.oppowner(Trigger.new);
}

Handler:
 
public class oppOwnerTriggerHandler {
    
    public static void oppowner(List<Opportunity> opplist){
        set<id> setAccountId = new set<id>();
    for(Opportunity opp: opplist){
        if(opp.AccountId !=Null){
            setAccountId.add(opp.AccountId); 
        }
    }
    Map<id,Account> mappaccidwithvalues = new map<id,Account>();
    
    for(Account acc:[select id,ownerid from Account where id=:setAccountId]){
        mappaccidwithvalues.put(acc.id,acc);
    }
    for (Opportunity op:opplist){
        if(mappaccidwithvalues.containskey(op.accountid)){
            op.ownerId = mappaccidwithvalues.get(op.accountid).ownerid;
        }
    }
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jishan,

The trigger will be as below.
 
trigger oppOwnerTrigger on Opportunity (before insert,before update) {
    oppOwnerTriggerHandler.oppowner(Trigger.new);
}

Handler:
 
public class oppOwnerTriggerHandler {
    
    public static void oppowner(List<Opportunity> opplist){
        set<id> setAccountId = new set<id>();
    for(Opportunity opp: opplist){
        if(opp.AccountId !=Null){
            setAccountId.add(opp.AccountId); 
        }
    }
    Map<id,Account> mappaccidwithvalues = new map<id,Account>();
    
    for(Account acc:[select id,ownerid from Account where id=:setAccountId]){
        mappaccidwithvalues.put(acc.id,acc);
    }
    for (Opportunity op:opplist){
        if(mappaccidwithvalues.containskey(op.accountid)){
            op.ownerId = mappaccidwithvalues.get(op.accountid).ownerid;
        }
    }
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,​​​​​​​
This was selected as the best answer
mukesh guptamukesh gupta
Hi Jishan,

I will recommend you below url that's will sharp you concept and you can do your self

https://www.mirketa.com/salesforce-developer-guide-use-of-helperhandler-class-to-manage-trigger-execution/

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh