+ Start a Discussion
KlivingstonKlivingston 

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 

             OR

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

      end if

end trigger

 

 

Many thanks in advance

Sonam_SFDCSonam_SFDC

Hi,

 

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

http://stackoverflow.com/questions/13701379/apex-code-to-send-email-alert

souvik9086souvik9086

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
mail.setHtmlBody(body);

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

 

If the post helps you please throw KUDOS.

NZ_XueNZ_Xue

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.

KlivingstonKlivingston

Thanks

 

 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.