instantiating an ob...
 
Notifications
Clear all

[Solved] instantiating an object within a trigger in salesforce

2 Posts
1 Users
0 Likes
483 Views
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

Can you instantiate an object within a trigger?

Issue Tags
1 Reply
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

Because code in a trigger is bound by Apex transaction, it is subject to governor limits. To reduce the incidence of governor limit exceptions, a Salesforce best practice is to avoid instantiating an object within a trigger. 

Instead, invoke a trigger handler from the trigger, and instantiate objects in the handler. When we use a trigger, we call the method in the trigger handler using the static keyword. With the static keyword, we avoid instantiating the class into an object, which helps to prevent governor limit exceptions.

For example, create the CreateContact trigger to invoke the createContact method:

trigger CreateContact on Candidate__c (after insert){
    /* Invoke the createContact method with a list of Candidates as the argument
    to create a corresponding Contact from each new Candidate Record */
    CreateContactFromCan.createContact(Trigger.new);
}

Learn more here: https://trailhead.salesforce.com/content/learn/projects/quick-start-apex-coding-for-admins/create-a-trigger

Reply
Share: