+ Start a Discussion
Lakshmi SLakshmi S 

Trigger Logic issue

Hi Team,

which is the best scenario for calling handler class

Scenario : 1
-------------------
trigger OpportunityTrigger on Opportunity (before insert, before update, before delete, after insert, after update) {
    
    if(Trigger.isBefore && Trigger.isInsert){
        
        // before insert.
        
    }
    
    if(Trigger.isBefore && Trigger.isUpdate){
        
        //  before update.
        
    }
    
    if(Trigger.isBefore && Trigger.isDelete){
        
         //  before delete
    }
    
}
Scenario : 2
trigger OpportunityTrigger on Opportunity (before insert, before update, before delete, after insert, after update) {
    
    if(Trigger.isBefore){
        
        if(Trigger.isInsert){
            
            // before insert
        }
        else
            if(Trigger.isUpdate){
                
                // before update.
                
            }
        
        else
            if(Trigger.isDelete){
                
              //  before delete
                
            } 
    }
        
}

Please let me know which is the best practice.

Thanks,
Lakshmi.

 
Best Answer chosen by Lakshmi S
Niraj Kr SinghNiraj Kr Singh
Hi Lakshmi,
I think you are looking for the trigger best approach. For that you can check with the following links. It will help you lots for that.

Trailhead:
https://trailhead.salesforce.com/en/modules/apex_triggers/units/apex_triggers_intro
Other helpful post:

https://developer.salesforce.com/forums/?id=906F0000000DBl8IAG

Thanks
niraj