0

How can I move the following trigger logic into an apex class?

trigger AssignAccNumber on Opportunity(before insert,before update){

List<Id> accIds = new List<Id>();

for(Opportunity opp:trigger.new){

    if(opp.AccountId!=null){

        accIds.add(opp.AccountId);
    }
}

Map<Id,Account> accMap = new Map<Id,Account>([SELECT id,Area__c,AccountNumber,Type FROM Account WHERE id in:accIds]);
Account a = [SELECT AccountNumber FROM Account WHERE AccountNumber != null AND Type='Prospect' AND Area__c='East' ORDER BY AccountNumber DESC LIMIT 1];

for(Opportunity opp :Trigger.new){
 Account accs = accMap.get(opp.AccountId);       

if(!accMap.IsEmpty()){

        if(opp.Probability == 95 && accs.AccountNumber == null){

            if(accs.LC_Market__c == 'Americas' && accs.Type == 'Prospect'){

                accs.AccountNumber = String.valueOf(Integer.valueOf(a.AccountNumber)+1);
            }            
        }
        update accMap.values();
    }
}            

}

1

You only need to pass the Opportunities from the new trigger to the new class

public class NameOfClass {
         List<Opportunity> triggerOpps = new List<Opportunity>();
         List<Id> accIds = new List<Id>();

         public NameOfClass(List<Opportunity> opps){
                 triggerOpps = opps;
         }

         for(Opportunity opp: triggerOpps ){
                if(opp.AccountId!=null){
                      accIds.add(opp.AccountId);
                }
         }

         Map<Id,Account> accMap = new Map<Id,Account>([SELECT id,Area__c,AccountNumber,Type FROM Account WHERE id in:accIds]);
         Account a = [SELECT AccountNumber FROM Account WHERE AccountNumber != null AND Type='Prospect' AND Area__c='East' ORDER BY AccountNumber DESC LIMIT 1];

         for(Opportunity opp :Trigger.new){
                 Account accs = accMap.get(opp.AccountId);       

                 if(!accMap.IsEmpty()){
                     if(opp.Probability == 95 && accs.AccountNumber == null){

                          if(accs.LC_Market__c == 'Americas' && accs.Type == 'Prospect'){
                                 accs.AccountNumber = String.valueOf(Integer.valueOf(a.AccountNumber)+1);
                          }            
                      }
                      update accMap.values();
                  }
          }

}

And in your trigger:

trigger AssignAccNumber on Opportunity(before insert,before update){
        NameOfClass noc = new NameOfClass(trigger.new);
}

Or you might want to wrap your trigger logic in a method that is inside the newly created apex class

public class NameOfClass {
       List<Id> accIds = new List<Id>();

       public void Execute(List<Opportunity> opps){
            for(Opportunity opp: triggerOpps ){
                 if(opp.AccountId!=null){
                        accIds.add(opp.AccountId);
            }


             Map<Id,Account> accMap = new Map<Id,Account>([SELECT id,Area__c,AccountNumber,Type FROM Account WHERE id in:accIds]);
             Account a = [SELECT AccountNumber FROM Account WHERE AccountNumber != null AND Type='Prospect' AND Area__c='East' ORDER BY AccountNumber DESC LIMIT 1];

             for(Opportunity opp :Trigger.new){
                     Account accs = accMap.get(opp.AccountId);       

                     if(!accMap.IsEmpty()){
                         if(opp.Probability == 95 && accs.AccountNumber == null){

                              if(accs.LC_Market__c == 'Americas' && accs.Type == 'Prospect'){
                                     accs.AccountNumber = String.valueOf(Integer.valueOf(a.AccountNumber)+1);
                              }            
                          }
                          update accMap.values();
                      }
              }
        }                        
    }

And then call the method from the trigger

trigger AssignAccNumber on Opportunity(before insert,before update){
            NameOfClass noc = new NameOfClass();
            noc.Execute(trigger.new);
}
0

Kon,

Force.com MVP Kevin O'Hara has created a framework to handle exactly this. Visit his github project here: https://github.com/kevinohara80/sfdc-trigger-framework. Follow the instructions in the README and you'll find moving your logic outside of your trigger isn't as complicated as you may think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.