+ Start a Discussion
Lindsay TackettLindsay Tackett 

account owner transfer email notification

Hi all,

Our Salesforce stopped sending notification emails when an account has been transferred (even if the 'Send Notificaiton Email' checkbox is marked) so I am trying to create an Apex Trigger that will send an email to the old and new account owner and I almost have it although I can't figure out how to pull in the Account Name so that they both know which account has been transferred.  The original emails send from Salesforce also included a link to the account record which would be nice but not neseccary.  

Here's what I have:
 
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('Household Owner Transfer   ');
      String body = 'The following Household has been transferred:';
      mail.setToAddresses(sendTo);
      mail.setHtmlBody(body);
      mails.add(mail);
      try{
      Messaging.sendEmail(mails);
      }
      catch(Exception e){
      system.debug('-------------exception------'+e);
       
      }
    
    }
    
    }
  
}

If anyone could help me include the Account Name (household for us), and a link to the account record that would be great!  Thanks!
Ravi Dutt SharmaRavi Dutt Sharma
Hey Lindsay,

You can create a map which will have account owner as the key and account name as the value. Using this map, you can get the account name in your mail body.