+ Start a Discussion
PRepakaPRepaka 

send email to multiple users using visualforce email template

hi

 

can u please tell me how to send email to multiple users by using visual force email template. if i send email to single user i am using setTargetObjectId(objContact.Id) and setWhatId(SR.Id). but i am not getting how to send to multiple users.

 

Please help me

PranatiPranati

Use this code

 

public void SendEmail(List<Id> userids)

{

Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();

List<Id> contactIds = new List<Id> ();

contactIds .add('003Q0000005DYvTIAW');

contactIds .add('003Q0000005DYvTIAW');

mail.setTargetObjectIds(contactIds); mail.setTemplateId('00XQ0000000HvBNMA0');

mail.setSaveAsActivity(false);

Messaging.SendEmailResult[] sendEmailResults = Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });

 

for (Messaging.SendEmailResult sendEmailResult : sendEmailResults) {

if(sendEmailResult.isSuccess()){ System.debug('success');

}

else{ System.debug(

'error');

}

}

}

Sam KanSam Kan

Hi

 

Thank u for sharing this code. Can u tell me,how to invoke this code in a button?  

khanWebgurukhanWebguru
blank_page

Dear All,

 

 I have an email template which is given below:

 

Evaluation Request from {!Lead.LastName} {!Lead.FirstName} for {!Lead.SupportProduct__c}, is pending and requires manual approval. <br/>

Click <a href="{!ApprovalRequest.Internal_URL}">here</a> to approve or reject this request.<br/><br/>

Thanks.

 

Whenever I use this template through "Approval Processes" it works fine. But I have different situation in which I cannot use "Approval Processes". For example I need that when a Lead create then email should be generate for Lead Approval and send to those receptionist that are belongs to selected region. If a lead belongs to ASIA then email should be send on asia@abc.com or if region is USA then it should be for usa@abc.com I successfully completd this task with static or just text base template. But whenever I use Customise template its not filling merge values. Following is TRIGER code:

 

trigger TestEmail on Lead (after insert)
{       
            MailerUtils.sendMail(Trigger.new[0].Region__c, String.valueOf(Trigger.new[0].Id), Trigger.new  [0].IsEmbargoe__c);


 

 

Now, following is MailerUtils class having sendMail method

 

public class MailerUtils
{
 
    public static void sendMail(string location, string leadId, Boolean IsEmbargoe)
    {
        string message;
        string temp = 'ApproveLeadTemplate';
        String[] toAddresses;
        List<Id> idsList = getEmailAddresses(location);
       
        if(IsEmbargoe == False)
        {
            temp = 'ApproveEmbargoeTemplate';
        }
        EmailTemplate e = [select Id,Name,Subject,body from EmailTemplate where name like :temp+'%'];
                                                  
        if(e != null || idsList==null)
        {
            Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
            mail.saveAsActivity = false;
   
            mail.setTargetObjectIds(idsList);
            mail.setTemplateId(e.Id);
           
            mail.setUseSignature(false);
            mail.setSaveAsActivity(false);


            // Send the email
            Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
          
        }
        else
        {
            Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
            toAddresses = new String[] {'khan@abc.com'};
            mail1.setToAddresses(toAddresses);
            message = 'This email will recieve by you only if Template Not Found!!!';
            mail1.setHtmlBody(message);
        }
                
            }  
   
     public static List<Id> getEmailAddresses(string groupName)
     {

        List<String> idList = new List<String>();
       
        List<Id> mailToIds = new List<Id>();
       
        Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = :groupName];
       
        for (GroupMember gm : g.groupMembers) {
       
        idList.add(gm.userOrGroupId);
       
        }
       
        User[] usr = [SELECT Id, email FROM user WHERE id IN :idList];
       
        for(User u : usr) {
       
        mailToIds.add(u.Id);
       
        }
       
        return mailToIds;
       
    }
   

 

 

I have some public group having same name like ASIA, USA and Austrailia so that this help me to pull autmatic all user from selected group on the bases of Region.

 

But In all this I am having problem that the dynamic fields or u can say merge fields are not filling please help me in this context. Thanking in advance for you help.

 

Regards,

 

Asif Ahmed Khan

Sr. Software Eng.

Palmchip :)