+ Start a Discussion
Prince PranavPrince Pranav 

Batch Apex Class On Account

Create a Batch Apex Class on Account object… Fetch all the Accounts… If Account Type = ‘
Customer - Direct’ then Update the all the child contacts Status field = ‘Signed Off’. Send an email after all the processing. This email should contain the statistics like Processed records count.
Best Answer chosen by Prince Pranav
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi 
Please try the below batch Class:
 
global class UpdateContactBasedOnAccount implements Database.Batchable<sObject>,Database.Stateful{
    global String query;   
    global UpdateContactBasedOnAccount(){
       query =  'SELECT Id, Name, Account_Status__c FROM Account where Type=Customer - Direct';
        
        
        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
         return Database.getQueryLocator(query);
        
       
    }
    
    global void execute(Database.BatchableContext BC, List<Account> scope){
        List<Id> accIds=new List<Id>();
        system.debug(scope);
        for(Account acc:scope)
        {
           accIds.add(acc.Id); 
        }
        List<Contact> conList=new List<Contact>([Select id,AccountId,Phone from Contact where AccountId in: accIds]);
         String[] toAddresses = new String[] {};
             for(Contact c:conList)
        {
            c.Status='Signing Off';
            toAddresses.add(c.Owner.Email);
            
        }
        try
        {
         Update conList;
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           
                
            mail.setToAddresses(toAddresses);
            mail.setPlainTextBody('Contact updated Successfully');
            
           
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });   
        }
       
        catch(Exception e)
        {
           system.debug(e); 
        }
        
    }
    
    global void finish(Database.BatchableContext BC){
        
  
    } 

}

Hope this will be helpful.
Thanks.​​​​​​​

All Answers

mirkimirki
Hi,

There is an excellent guidance on how to use Batch Apex in Trailhead. Check the section "Using State in Batch Apex" to get the idea on how to calculate the processed records.
https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch

Email sending is very well described in the following reference document:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

With these resource you should be able to proceed.

Regards,
mirki
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi 
Please try the below batch Class:
 
global class UpdateContactBasedOnAccount implements Database.Batchable<sObject>,Database.Stateful{
    global String query;   
    global UpdateContactBasedOnAccount(){
       query =  'SELECT Id, Name, Account_Status__c FROM Account where Type=Customer - Direct';
        
        
        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC){
         return Database.getQueryLocator(query);
        
       
    }
    
    global void execute(Database.BatchableContext BC, List<Account> scope){
        List<Id> accIds=new List<Id>();
        system.debug(scope);
        for(Account acc:scope)
        {
           accIds.add(acc.Id); 
        }
        List<Contact> conList=new List<Contact>([Select id,AccountId,Phone from Contact where AccountId in: accIds]);
         String[] toAddresses = new String[] {};
             for(Contact c:conList)
        {
            c.Status='Signing Off';
            toAddresses.add(c.Owner.Email);
            
        }
        try
        {
         Update conList;
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           
                
            mail.setToAddresses(toAddresses);
            mail.setPlainTextBody('Contact updated Successfully');
            
           
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });   
        }
       
        catch(Exception e)
        {
           system.debug(e); 
        }
        
    }
    
    global void finish(Database.BatchableContext BC){
        
  
    } 

}

Hope this will be helpful.
Thanks.​​​​​​​
This was selected as the best answer