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
Waseem AkramWaseem Akram 

Apex Class to send Email Notification when Account Owner is Changed

Hi All,
          I want to send an email notification when account owner is changed. And also i want to send email notification to both old account owner and new account owner which has been changed. How to achieve this. Can anyone send me the code?
Best Answer chosen by Waseem Akram
RAM AnisettiRAM Anisetti
One more option added
Use trigger...best one is workflow
 
trigger Sampleemailtrigger on Account (after update) {
  
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  List<ID>ownerids=new List<ID>();
  
  List<String> sendTo = new List<String>();
  List<User>users=new List<User>();
  
  for (Account myacc : Trigger.new) {
  
    Account oldcon = Trigger.oldMap.get(myacc.Id);
    if (myacc.ownerid != oldcon.ownerid ) {
    
       ownerids.add(myacc.ownerid) ;  
       ownerids.add(oldcon.ownerid) ;
       
      
    }
    }
    
    if(ownerids.size()>0 ){
      
    users=[select name,id,email from user where id in:ownerids];
    system.debug('-------------users------'+users);
    if(users.size()>0){
     for(User u:users){
      sendTo.add(u.Email);
    }
    
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     //mail.setReplyTo('ramanisetti@gmail.com');
      mail.setSenderDisplayName('Email alert');

      mail.setSubject('Owner change');
      String body = 'Dear User Owner changed';
      mail.setToAddresses(sendTo);
      mail.setHtmlBody(body);
      mails.add(mail);
      try{
      Messaging.sendEmail(mails);
      }
      catch(Exception e){
      system.debug('-------------exception------'+e);
       
      }
    
    }
    
    }
  
}

 

All Answers

Suraj GharatSuraj Gharat
Hi Waseem,

You may accomplish this using a workflow rule. This workflow would have an appropriate condition for its evaluation criteria and will have three immediate actions as follows:
  1. Field Update: For this, first add a new email field, and populate it here with old owner's email address (Use PRIORVALUE (https://help.salesforce.com/apex/HTViewHelpDoc?id=customize_functions_i_z.htm&language=en_US#PRIORVALUE) function).
  2. Email Alert: Send email to old owner, with "Recipient Type" equal to "Email Field" and then select above field.
  3. Email Alert: Send email to new owner.
Thanks,
Suraj
RAM AnisettiRAM Anisetti
One more option added
Use trigger...best one is workflow
 
trigger Sampleemailtrigger on Account (after update) {
  
  List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
  List<ID>ownerids=new List<ID>();
  
  List<String> sendTo = new List<String>();
  List<User>users=new List<User>();
  
  for (Account myacc : Trigger.new) {
  
    Account oldcon = Trigger.oldMap.get(myacc.Id);
    if (myacc.ownerid != oldcon.ownerid ) {
    
       ownerids.add(myacc.ownerid) ;  
       ownerids.add(oldcon.ownerid) ;
       
      
    }
    }
    
    if(ownerids.size()>0 ){
      
    users=[select name,id,email from user where id in:ownerids];
    system.debug('-------------users------'+users);
    if(users.size()>0){
     for(User u:users){
      sendTo.add(u.Email);
    }
    
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
     //mail.setReplyTo('ramanisetti@gmail.com');
      mail.setSenderDisplayName('Email alert');

      mail.setSubject('Owner change');
      String body = 'Dear User Owner changed';
      mail.setToAddresses(sendTo);
      mail.setHtmlBody(body);
      mails.add(mail);
      try{
      Messaging.sendEmail(mails);
      }
      catch(Exception e){
      system.debug('-------------exception------'+e);
       
      }
    
    }
    
    }
  
}

 
This was selected as the best answer
Waseem AkramWaseem Akram
Thanks a lot RAM..Your Code was really helpful..
Waseem AkramWaseem Akram
Thanks suraj for giving response.
NyshaaNyshaa

Hi Suraj,

Can you please elaborate the workflow process to achieve this.

Best, Nisha.

Suraj GharatSuraj Gharat
Hi Nyshaa,

I haven't worked on Force.com anymore since the last 4-5 years, and so my solution may not be the best possible way to do this.

In order to send an email using workflow, we need either a field of type Email on the record that triggered the flowflow, or a field of type User (Lookup to User) on the record that triggered the flowflow.

Here we want to send two emails, one to the old owner (Let's call this email A) , and another one to the new owner (Let's call this email B). 
For email A, somehow we need to store the old owner's id onto the same record, so that we could use it in a workflow email action. In order to do that, we need an additional field of type Lookup (User), and a workflow action of type Field Update. The Field Update action can make use of the PRIORVALUE function to get the old owner id and set it in the new field that we would add. This Field Update action must get executed before actual email A workflow action that would send the email to the old owner.

For email B, it is rather simple as we already have a new owner into the Owner field, and so one workflow email action would do the job.

In summary, what we need is:
1. One custom lookup field to store old owner
2. One worflow rule with three actions as below
3. First workflow action - Field Update - sets field added in #1 with the old owner id
4. Second workflow action - Email - sends email to #1 field user
5. Third workflow action - Email - sends email to Owner field user

Please let me know if you need more information.

Thanks,
Suraj Gharat