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


Through batch apex updating records
What ever mail id field  present in lead object to those mail ids email should be sent through bacth apex how is it achieved

Thanks in advance

mukesh guptamukesh gupta
Hi Prince 

Email   this filed will have lead email id, please use Email in your code

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Prince VenkatPrince Venkat
Hi Mukesh
Thanks for your quick reply

can you be more clear please i did nto get you

SwethaSwetha (Salesforce Developers) 
HI Teja,
The code snippet mentioned in should help you get started. It is in the context of fetching email id from contact object. You will need to customize this as per your requirement.
global class EmailToContactBatch implements Database.Batchable<sObject> {
    global database.querylocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator([select id,name,Email from Contact where id='00328000015TLBD']);
    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) {
            //Prepare CCEmailList
            List<String> lstCCToEmails = new List<String>();
            if(objContact.Email != null) {
            // Set From Email Address
            //objEmail.setOrgWideEmailAddressId('Organisation Wide Email Address Id');
            //Set Email Subject
            objEmail.setSubject('Testing Emails');
            //Set Email Template
            //objEmail.setTemplateId('Email Template Id');
            //Set Email Body
            String body = 'Dear Contact,please ready to contact if you have any issues';
        if(!Test.isRunningTest()) {
            Messaging.SendEmailResult[] results = Messaging.sendEmail(lstEmails);
            // To Check whether Email is sent or not,We update processed field of each contact.
            if(results[0].success) {
                List<Contact> lstContactToUpdate = new List<Contact>();
                for(Contact objContact :(List<Contact>)scope) {
                    objContact.Processed__c = true;
                update lstContactToUpdate ;
                system.debug('lstContactToUpdate:'+lstContactToUpdate );
    global void finish(Database.BatchableContext BC) {

If this information helps, please mark the answer as best. Thank you
Suraj Tripathi 47Suraj Tripathi 47
Hi Prince,

1. Please Create a class that implements an interface Database.batchable.
2. Overload 3 methods - start(), execute and finish().
3. Create an Instance variable of List<String>.
3. In the start method, query those records of lead which having email does not equal to NULL.
4. In execute, add those emails to an instance variable.
5. In the finish, send mail to those email IDs which are present in instance variable using SingleEmailMessage Class.

If you find your Solution then mark this as the best answer. 

Thank you!

Suraj Tripathi