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
mallikammallikam 

Can triggers have methods?

If so, please provide an example.

 

thanks!

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

Yes, you can write the email logic in your controller extension class.

 

No, you can't write methods directly in a trigger. But the trigger can instantiate an object from another class and invoke public methods from there. 

All Answers

paul-lmipaul-lmi
you're better off managing that code in a class separately from the trigger.
mallikammallikam

thanks, Paul. I have to send a email as soon as a case is closed. Right now I have the email sending code

right in the trigger. Can I write the same code in my controller extension class instead and call it from trigger?

aalbertaalbert

Yes, you can write the email logic in your controller extension class.

 

No, you can't write methods directly in a trigger. But the trigger can instantiate an object from another class and invoke public methods from there. 

This was selected as the best answer
mallikammallikam
that clears my doubt, thanks Albert!