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
Samadhan Sakhale 3Samadhan Sakhale 3 

Batch Class

Hi,

i'm new to batchable class someone help me to find all contacts which does not have account in salesforce and how to send email to that contact account mail-ID
Best Answer chosen by Samadhan Sakhale 3
MithunPMithunP
Hi Samadhan Sakhale 3,

You can try below batch class, Note that we have Single Email Message Limitations in salesforce (Ex: for developer edition 10/day)

Batch Class
global class batchContactUpdate implements Database.Batchable<sObject> {
    global static String[] emailIds = new String[]{};
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name,email FROM Contact WHERE accountid = null';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Contact> conList) {
         emailIds = new List<String>();
         system.debug('+++emailIds+++'+emailIds);
         for(Contact con : conList)
         {
             if(con.email != null){
                 emailIds.add(con.email); 
             }           
         }
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           system.debug('+++emailIds+++'+emailIds);
           mail.setToAddresses(emailIds);
           mail.setSubject('Associated Account Not Found');
           mail.setPlainTextBody('Associated Account Not Found');
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }   
    
    global void finish(Database.BatchableContext BC) {
           
    }
}

Execute below code from Developer Console Anonymous execute window

batchContactUpdate bt = new batchContactUpdate();
database.executeBatch(bt);
Best Regards,
Mithun.
 

All Answers

MithunPMithunP
Hi Samadhan Sakhale 3,

Can you provide more details about your requirement. what is meant by contact account mail-ID.

Best Regards,
Mithun.
Samadhan Sakhale 3Samadhan Sakhale 3
hi Mithun,

my requirement is to take emailId's from Contact Standard field who didnt assigned any Account means who's AccountId is blank and send mails to that Users

Regards,
Sam
MithunPMithunP
Hi Samadhan Sakhale 3,

You can try below batch class, Note that we have Single Email Message Limitations in salesforce (Ex: for developer edition 10/day)

Batch Class
global class batchContactUpdate implements Database.Batchable<sObject> {
    global static String[] emailIds = new String[]{};
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name,email FROM Contact WHERE accountid = null';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Contact> conList) {
         emailIds = new List<String>();
         system.debug('+++emailIds+++'+emailIds);
         for(Contact con : conList)
         {
             if(con.email != null){
                 emailIds.add(con.email); 
             }           
         }
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           system.debug('+++emailIds+++'+emailIds);
           mail.setToAddresses(emailIds);
           mail.setSubject('Associated Account Not Found');
           mail.setPlainTextBody('Associated Account Not Found');
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }   
    
    global void finish(Database.BatchableContext BC) {
           
    }
}

Execute below code from Developer Console Anonymous execute window

batchContactUpdate bt = new batchContactUpdate();
database.executeBatch(bt);
Best Regards,
Mithun.
 
This was selected as the best answer
Samadhan Sakhale 3Samadhan Sakhale 3
thanks Mithun sir,

Thanks a lot..........

Sam
MithunPMithunP
I'm Glad It helped.

Best Regards,
Mithun.