+ Start a Discussion
Anil DuttAnil Dutt 

Send Email To multiple users

Hi,

 

i have a situation where i need to send email to multiple users with an email template

 

please check the below link, its mentioned there, to use email template we always need to set setTargetObjectId to Contact.Id

 

http://www.forcetree.com/2009/07/sending-email-from-your-apex-class.html

 

so to send email to two users we need to create two contacts first then after use delete like this

 

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                
            Contact con =  new Contact();
            con.FirstName = 'Anil';
            con.LastName = 'Dutt';
            con.Email = 'anil@swiftsetup.com';
            insert con;
        
            mail.setTargetObjectId(con.id);
            mail.setWhatId(it.Id);
            mail.setTemplateId('00Xd0000000PIwh');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            delete con;

 

 

            Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
            

            Contact con1 =  new Contact();
            con1.FirstName = 'Anil';
            con1.LastName = 'Dutt';
            con1.Email = 'anildbest83@gmail.com;
            insert con1;
        
            mail1.setTargetObjectId(con1.id);
            mail1.setWhatId(it.Id);
            mail1.setTemplateId('00Xd0000000PIwh');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail1 });
            delete con;

 

Now above code works fine , send email to both the users but

 

Firstly, this becomes bulky if number of users are very large

2nd, in the chatter, it shows

           Anil Dutt - anild created this contact

           Anil Dutt - anild created this contact

 

so above code create contact and then delete but it shows in chatter that a contact has been created

 

Is there any better way to accomplish above?

 

Please advise

 

Thanks

mohimohi

settargetobjectId can take user ,lead or contact. id.create single instance of mail lik mail1, mail2,mail3 and add those to list of single mail and then use send mail..hope this will help u ,if you got insight please mark as accepted.

Anil DuttAnil Dutt

@mohi

 

i think i m doing exactly what you are saying

 

will you please tell me what should i change in my code?

mohimohi

anil ,please remove what id and assign user id to settargetobjectid .hope this will help you

Anil DuttAnil Dutt

Hi mohi,

 

whenever some thing happened it shows in chatter , so as i m creating contact first to set setTargetObjectId to Contact.Id it  shows in chatter that a contact has been created.

Is there any way to send email using email template to multiple users without using contacts?

SamuelDeRyckeSamuelDeRycke

You can also set the target email(s) via a string value, have a look at the documentation. The use of contacts is mainly to use with existing contacts I'd say.

 

Outboud emails , SingleEmailMessage methodsMassEmailMessage methods

 

 

Anil DuttAnil Dutt

Hello Sdry,

 

In case we don't have contact and want to send email to some other users who are not in the system then how we can send email to those users?

 

SamuelDeRyckeSamuelDeRycke

My message contains 2 links to the salesforce documentation ... did you look at that ?  To me, the technical documentation is always the first start point of development. How can you develop if you're not sure of what is possible.

Anil DuttAnil Dutt

Yes Sdry

 

i looked in documentation 

 

your example is without using Email template , i need to use Email Template,  to use email template we always need to set setTargetObjectId to Contact.Id and so the all story i wrote in my question, hope you got my point

SamuelDeRyckeSamuelDeRycke

You're right, I did answer and read a bit too hasthy. But the answer is still in the documentation, and is pretty much what you expected: It does not seem to be possible at this time.