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
VioletViolet 

How to create a batch class to just update the Contacts defined in a trigger?

I have a trigger on Account that updates all child Contact objects but I am hitting a DML error due to number of Contacts on the Account. I want to call a batch class from the trigger if the list of Contacts is larger than 10000 (DML limit). 

Is there a way to define the batch class in a way that it just takes the list of Contacts and updates them in batch? This is what I have for the batch and it is giving bunch of errors
 
global class SG_BatchContactUpdate implements Database.Batchable<sObject>{

  Map<Id, Contact> contactmap = new Map<Id, Contact>();
    global SG_BatchContactUpdate (Map<Id, Contact> contacts) {
        contactmap=contacts;
        
         }
global Database.QueryLocator start(Database.BatchableContext BC) {
return DataBase.getQueryLocator([SELECT Id FROM Contact WHERE Id IN : contactmap.keySet()]);
}
    
    //Execute Method.
    global void execute(Database.BatchableContext BC) {
        
    }
}

This is how I am calling from the trigger
Database.executeBatch(new SG_BatchContactUpdate(contactsToUpdate));

 
Best Answer chosen by Violet
VioletViolet
if anyone is curious, this solved it
global class BatchContactUpdate implements Database.Batchable<sObject>{

    List <Account> mapAccount = new List <Account> ();
    List <Contact> contactlist1 = new List <Contact> ();
    
    global BatchContactUpdate(List <Account> AccountUpdate) {
        mapAccount=AccountUpdate;
        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return DataBase.getQueryLocator([SELECT Id, AccountID
                                         FROM Contact
                                         WHERE AccountID IN :mapAccount
                                        ]);
    }
    global void execute(Database.BatchableContext BC , List <Contact> contactlist) {
        for (Account acct : mapAccount){ 
            for (Contact con : contactList){
                if (con.AccountID == acct.Id){
                        contactlist1.add(new Contact(
                            Id = con.Id,
                           );
                    }
            }   
        }
        
         update contactlist1;
    } 
    global void finish(Database.BatchableContext BC){
        
    }
}

All Answers

deepak balur 19deepak balur 19
Can you also post how you are calling the Batchable from within Account please? I mean, is it in a LOOP is what I want to see.
VioletViolet
if (contactsToUpdate.isEmpty()){
        try{
            
            if (contactsToUpdate.size()>1000)
            {
             BatchContact_Update(contactsToUpdate);

            }
            
            else {
                update contactsToUpdate;

           }
Thanks for the reply, it is not in the loop. I just  want to use the batch if the list if more than 10000 because it fails after 10000
VioletViolet
if anyone is curious, this solved it
global class BatchContactUpdate implements Database.Batchable<sObject>{

    List <Account> mapAccount = new List <Account> ();
    List <Contact> contactlist1 = new List <Contact> ();
    
    global BatchContactUpdate(List <Account> AccountUpdate) {
        mapAccount=AccountUpdate;
        
    }
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return DataBase.getQueryLocator([SELECT Id, AccountID
                                         FROM Contact
                                         WHERE AccountID IN :mapAccount
                                        ]);
    }
    global void execute(Database.BatchableContext BC , List <Contact> contactlist) {
        for (Account acct : mapAccount){ 
            for (Contact con : contactList){
                if (con.AccountID == acct.Id){
                        contactlist1.add(new Contact(
                            Id = con.Id,
                           );
                    }
            }   
        }
        
         update contactlist1;
    } 
    global void finish(Database.BatchableContext BC){
        
    }
}
This was selected as the best answer