+ Start a Discussion

Email Trigger

Hi All


I would like to email to a person other than Account Owner when the account owner changes. Unfortunately it is not possible unless using Apex code.


I would be glad if you could help me write code to do the job for me. Here is pseudo code of what I am tying to achieve:


Trigger SendEmailToRegionalManager on Site_Defects__C after update

       if owner of Site_Defect__C record changed then

              get the name of Regional Manager of that record

              match the name to user and get the email address

              (I am not sure which one of the following is easier)

              get the email from email template change the recipient to Regional Manger send the email 


             use Mail. Function to crate email body here and then send it

      end if

end trigger



Many thanks in advance




The following post has the code snippet you can update to fit the requirement:



Trigger SendEmailToRegionalManager on Site_Defects__C(after update) {

if(Trigger.new[0].OwnerId != Trigger.old[0].OwnerId) {
//Sending Mail
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage() ;

//Setting user email in to address

String email = Trigger.new[0].RegionalManager__c;
String[] toAddresses = new String[] {email} ;

// Assign the addresses for the To and CC lists to the mail object
mail.setToAddresses(toAddresses) ;

//Email subject to be changed
mail.setSubject('Your Subject');
String body = 'Your Body';
//Body of email

//Sending the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


If the post helps you please throw KUDOS.


are you asking a trigger code to take a string value of field "Regional Manager" and then search on user object?

if so why do not create a look up field "Regional Manager" to user.
 in this way you can use standard email alert action by select Recipient Type "realted user". ofcasue you need a workflow on owner change.




 NZ_Xue  Thanks for your suggestion. The Regional Manager is just a filed in our Site Defect Object, so I don't think that is feasible do so.