Monday, 24 February 2014

How to send mass emails to users in salesforce

In the below sample code "lstPortalUser" is a list of partner users.Now I need to sent them an email with different body.

  1. List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();  
  2. for(User portalUser :lstPortalUser)  
  3. {     
  4.    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();  
  5.    string body = 'Hi '+ portalUser.LastName;    
  6.    mail.setSubject('Test Subject');  
  7.    mail.setTargetObjectId(portalUser.Id);  
  8.    mail.setSaveAsActivity(false);  
  9.    mail.setHtmlBody(body);  
  10.    mails.add(mail);  
  11. }  
  12. Messaging.sendEmail(mails);  

No comments:

Post a Comment