+ Start a Discussion
Cloud AtlasCloud Atlas 

Sending 400 emails using custom button

I am trying to create a custom button on List View (Lightning) for sending emails to over 400 people ... All emails will contain personalized info so doing a cc and bulkifying email recepients isn't an option.

I have a custom button on a list view ( which is made up of a VF page) and has ability to select upto 400 records.
The controller for that page is working on a for-loop , but I still ended up with sendMail limit error (cannot execute more than 10 sendMail)

User-added image
public class ExpertEmailController {
   public sObject[] mAct; 
   ApexPages.StandardSetController selectedRec;
   String redirectUrl = System.Label.Redirect_Url;
   public ExpertEmailController(ApexPages.StandardSetController controller){
       selectedRec = controller;
    public PageReference sendMail(){
        // Send Email
        for ( Monthly_Activity__c ma : (Monthly_Activity__c[])selectedRec.getSelected() ){
        mAct = [SELECT Email
                FROM sObject
                WHERE Id =: ma.Id 
                LIMIT 1];
        // Create body
            string body = 'Dear '+ mAct[0].User +', '+'\n\n';
            body += 'BODY'+'\n\n';

	    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(new String[] {mAct[0].Email});
            mail.setSenderDisplayName('Salesforce Administrator');
            Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{mail});   
        PageReference page = new PageReference(redirectUrl);
        return page;

Not exactly sure how an I circumvent this limit.
Creating an out-of-the-box (WF based) email alert on a value update isn't also an option since the records will be locked.

If anyone has any idea, let me know.
Thanks in advance.
Raghu NaniRaghu Nani
Add below code before for loop
List<Messaging.SingleEmailMessage> myEmails = new List<Messaging.SingleEmailMessage>();

Replace below code with Highlighted code
 Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{mail});

Add below code after for loop
Raghu NaniRaghu Nani
Your Soql also need to move out side of forloop, looks like you have to use map or something like that to extract Email in email body