Can you instantiate an object within a trigger?
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