+ Start a Discussion
Simha YadavSimha Yadav 

Send email using apex while updating record.

Can anybody give code for below scenerio?
1.Send email using apex while updating record.
  - create trigger on after update
  - create class and write logic to send email
  - call class from trigger
ManojjenaManojjena
Hi Simha,

Check below code sample and try to implement.
 
Trigger EmailTrigger on sObjectName(after  update){
  EmailTriggerTriggerHamndler.sendEmail(Trigger.new,Trigger.old);
} 
public class EmailTriggerTriggerHamndler{
  public static void  sendEmail(List<Sobject> newList,List<Sobject>  oldList){
      //Iterate the list and do the implementation here 
  
  }
}
Check the below link to send email  and leg me know any issue .
http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/



 
Vijay NagarathinamVijay Nagarathinam
Hi Simha,

Use the code in your class, I think it will be helpful for you.
 
EmailTemplate  template;
                                       try 
                                       {
                                           template = [select id, name from EmailTemplate where name = 'TestTemplate'];
                                       }
                                       catch(Exception e)
                                       { }
                                       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                                      String[] toAddresses = new String[] {leadRecord.CreatedBy.Email};
                                      String[] replyToAddresses = new String[] {leadRecord.CreatedBy.Email};
                                      mail.setTemplateId(template.Id); 
                                      mail.setTargetObjectId(leadRecord.OwnerId);
                                      mail.saveAsActivity = false;
                                      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
                                   }


 
Simha YadavSimha Yadav
am getting following error please check in attached file
ManojjenaManojjena
Hi Simha,

You forgot to attach file 
Simha YadavSimha Yadav
PFA
ManojjenaManojjena
Still missing 
Simha YadavSimha Yadav
please check it properly
ManojjenaManojjena
Are you attaching an image or text .Not able to view .
Simha YadavSimha Yadav
its ok tel me any perfect tutitorial to learn SOQL
ManojjenaManojjena
Hi Simha ,

Click on below link an down load the PDF it will help for sure !
http://www.salesforce.com/us/developer/docs/soql_sosl/salesforce_soql_sosl.pdf   

Thanks
Manoj