+ 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!