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
Eager-2-LearnEager-2-Learn 

Trigger Best Practice

HI,

I am wondering if my approach on programming a trigger and calling a class is the best approach.  This is what I am coding.

 

I created a class (not static).  In the class I have public member variables that are set from the trigger that intantiates the class.  These variables hold things like Trigger.New, isUpdate, isInsert, etc.  In the trigger I use IF logic to detect the type of trigger even and then instantiate the class and set the member variables accordingly.  Below is an example.  What would your feedback, best practices say about this approach?  Am I on target or are there flaws with this approach and if so what do you suggest?  Thank you in advance for your assistance.

 

trigger Contact_trigger on Contact ( after insert, after delete, after update, before insert, before update, before delete ) {   
    EmployerGroupPortal egp = new EmployerGroupPortal();
    egp.isInsert = Trigger.isInsert;            
    egp.isUpdate = Trigger.isUpdate;
    egp.isDelete = Trigger.isDelete;
    egp.oldMap = Trigger.oldMap;
    if ( Trigger.isBefore ) {
        if ( Trigger.isInsert || Trigger.isUpdate ) {            
            egp.contacts = Trigger.new;
        }  
        if ( Trigger.isDelete ) { 
            egp.contacts = Trigger.old;
        }
        egp.egpValidation();
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
digamber.prasaddigamber.prasad

Hi,

 

I can proudly say that you are on target and following the design patterns suggested by salesforce.

 

Happy to help you!

All Answers

digamber.prasaddigamber.prasad

Hi,

 

I can proudly say that you are on target and following the design patterns suggested by salesforce.

 

Happy to help you!

This was selected as the best answer
Eager-2-LearnEager-2-Learn

Hi Digamberlucky,

 

Thank you for the response and the A.  I wanted to know what would happen if I was over the 200 batch record count given that I am instantiating the class each time.  Would that be an issue of assuming that if I had 201 records the class would instantiate twice?

 

digamber.prasaddigamber.prasad

Hi,

 

Yes you are right, the class would instantiate twice if there are more than 200 records, and it is expected behavior. If you don't want to instantiate class, you can make your method static. But, it all depends upon your implementation.

 

If my previous reply worked for you, will you be kind enough to give me KUDOS for that.

 

Happy to help you!