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
mohan s 37mohan s 37 

How to send mass email using apex with emailTemplate

public class MassEmail{
public void show(){
EmailTemplate et=[select id from EmailTemplate where name ='approve loan'];
Messaging.MassEmailMessage email1=new Messaging.MassEmailMessage();
email1.setTemplateId(et.Id);
Contact c =[select id,email from Contact limit 60];
List<id>whoids=new List<id>{c.id};
email1.setTargetObjectIds(whoids);
    
Messaging.Email[] em=new Messaging.Email[]{email1};
Messaging.sendEmail(em);
}
}
This is my code.My intension is to send email to list of contacts.i am getting list of  emails from contacts.While executing the above code i was get issue like "System.QueryException: List has no rows for assignment to SObject". Can any one find what mistake i have done. 
nikita dhamalnikita dhamal
Hi mohan,
                  check if your who ids has ids in it..
                   The exception is because of you are not getting any record in your list
Dhriti Moulick 16Dhriti Moulick 16
EmailTemplate et=[select id from EmailTemplate where name ='approve loan'];

There are two concern:

1.Please re check the name ='approve loan' from email template which you are using in Query.If Name mis matches you will get the above error.

2.Re write your query as follows:
  
EmailTemplate et=[select id from EmailTemplate where DeveloperName ='approve_loan'];

DeveloperName is the API name for the particular email tmeplate you want to fetch.Because in near future you might change the name of the Email Template ,that time your Query wont work,but your DeveloperName,i.e. API Name remains same.

Please let me know you have any concern.
jay saijay sai
Hi,

If you want to send mass email along with email Template then Massmailer is perfect for you.

MassMailer Make Salesforce Email Limitations a Thing of the Past.
You can track your emails ,save and edit email templates on Salesforce when using MassMailer.
 By monitoring the performance of your email templates, you can optimize your past templates to improve results 
and avoid creating the same template more than once. Save time and get better results with MassMailer.

You can try this app by installing from appexchange -  Massmailer app

You can learn more details about the product on this website massmailer.io