function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Victor EcheverríaVictor Echeverría 

Send email template through apex to various contacts

I want to send an email message using a template to all contacts related to an account when a certain event happens in the account. For this I have created a trigger. The issue is with my "mail.setTargetObjectIds(lstIds)" statement. I recieve the following error: Method does not exist or incorrect signature: void setTargetObjectIds(List<Id>) from the type Messaging.SingleEmailMessage.

The variable lstlds is a list of ids I got by quering all related contacts of the account.What could the error be? Can't I us a list on the setTargetObjectIds function?
Best Answer chosen by Victor Echeverría
NagendraNagendra (Salesforce Developers) 
Hi Victor,

Please find the sample code which will send email to various contacts using apex.
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Your template Name'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
for(contact con : contactList)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id); 
mail.setTargetObjectId(con>id)
mail.setSaveAsActivity(false);
allmsg.add(mail);
}
Messaging.sendEmail(allmsg,false);
Basically, add all contact in the list and then send them in a single shot. At one time you can send 100 emails.

I would also suggest you please check with below link which might help you further. Please let us know if this helps.

Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra
 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Victor,

Please find the sample code which will send email to various contacts using apex.
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Your template Name'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
for(contact con : contactList)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id); 
mail.setTargetObjectId(con>id)
mail.setSaveAsActivity(false);
allmsg.add(mail);
}
Messaging.sendEmail(allmsg,false);
Basically, add all contact in the list and then send them in a single shot. At one time you can send 100 emails.

I would also suggest you please check with below link which might help you further. Please let us know if this helps.

Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra
 
This was selected as the best answer
Shephali SwarnkarShephali Swarnkar
Hi Nagendra,

      can you help me in the following code. I want to send email to selected contact's email id using wrapper class:
public with sharing class email {

    public List<wrpclass> lstwrpclass{get;set;}
    
    public email()
    {
        lstwrpclass = new List<wrpclass>();
        List<Contact> lstcon = [Select FirstName, Id, Email from Contact];
        for(Contact c : lstcon)
        {
           wrpclass obj = new wrpclass(c);
           lstwrpclass.add(obj);
        }
    }
    
    public void SendEmail()
    {
         
         ////what to write here??       
            
    }
    public class wrpclass{
       Public Contact con{get;set;}
       public boolean isSelected{get;set;}
       
       public wrpclass(Contact cn)
       {
         this.con = cn;
         this.isSelected = false;
       }
    }
}

Your help is awaited

Thanks
Shephali​​​​​​​