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
Prince VenkatPrince Venkat 

Batch Email code

Hi

Below is my code.Send mails to mailid field of contact object to three members at a time. Getting an error. System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Add a recipient to send an email.: []

global class EmailToContactBatch implements Database.Batchable<sObject> 
{
    global database.querylocator start(Database.BatchableContext BC) 
    {
        return Database.getQueryLocator([select id,name,Email from Contact order by Email asc limit 3]);
    }
   
    global void execute(Database.BatchableContext BC, Sobject[] scope) 
    {
        List<Messaging.SingleEmailMessage> lstEmails = new List<Messaging.SingleEmailMessage>();
        for(Contact objContact :(List<contact>) scope) 
        {
            Messaging.SingleEmailMessage objEmail = new Messaging.SingleEmailMessage();
            //Prepare SendToEmail List          
            List<String> lstSendToEmails = new List<String>();
            if(objContact.Email != null) 
            {
                lstSendToEmails.add(objContact.Email);
            }
            objEmail.setToAddresses(lstSendToEmails);
            //Prepare CCEmailList
            List<String> lstCCToEmails = new List<String>();
            if(objContact.Email != null) 
            {
                lstCCToEmails.add(objContact.Email);
            }
            objEmail.setCcAddresses(lstCCToEmails);
            
            //Set Email Subject
            objEmail.setSubject('Testing Emails');
           
           //Set Email Body
            String body = 'Dear Contact,please ready to contact if you have any issues';
            objEmail.setHtmlBody(body);
            lstEmails.add(objEmail);
            
        Messaging.sendEmail(lstEmails);
        }
    }

    global void finish(Database.BatchableContext BC) 
    {
        
    }
}

Thanks In advance
 
CharuDuttCharuDutt
Hii Prince Venkat
Try Below Test Class
@isTest 
public class EmailToContactBatchTest {
 @isTest    
    public static void testit() {
        List<Contact> lst = new List<Contact>();
        
			Account Acc = new Account();
        Acc.Name = 'testAcc';
        insert Acc;
        for (Integer i = 0; i<200; i++) {
            Contact l = new Contact();
            l.LastName = 'Test'+i;
            l.LastName = Acc.Name + i;
            l.AccountId = Acc.Id;
            l.Email = 'test'+i+'@test.Com';
            lst.add(l);
        }
        insert lst;
        
        test.startTest();
        
        EmailToContactBatch lp = new EmailToContactBatch();
        Id batchId = Database.executeBatch(lp);
        Test.stopTest();
            
    }
}
Please Mark it As Best Answer If It Helps
Thank You!

 
Prince VenkatPrince Venkat
Hi CharuDutt
Thanks for your reply
But its not about testclass

whenever i am using the above getting the error like
System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Add a recipient to send an email.: []

 
Prince VenkatPrince Venkat
Hi can Anyone Help to avoid the error
CharuDuttCharuDutt
Hii Prince 
Try Below Code
global class EmailToContactBatch implements Database.Batchable<sObject> 
{
    global database.querylocator start(Database.BatchableContext BC) 
    {
        return Database.getQueryLocator([select id,name,Email from Contact order by Email asc limit 3]);
    }
   
    global void execute(Database.BatchableContext BC, list<contact> scope) 
    {
        list<String> toAddresses =new List<String>();
        for(Contact objContact :scope) 
        {
            if(objContact.Email != null){
           toAddresses.add(objContact.Email);
               }
        }
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       
        mail.setToAddresses(toAddresses);
        mail.setSubject('Testing Emails');
        mail.setPlainTextBody('Dear Contact,please ready to contact if you have any issues');
        
        
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        
       
    }

    global void finish(Database.BatchableContext BC) 
    {
        
    }
}
Please Mark it As Best Answer If It Helps
Thank You!
 
 
asg aseasg ase
What is the best way integrate the scripts in dynamic hosted website? I want to use for my lock service (http://leicesters-locksmiths.co.uk/) website.