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
speedforcespeedforce 

Limit on updating mass records using apex batch

I have written one batch apex which will update contacts and records. There are about 2 million records which should be updated by this batch everytime it runs. Is it recommended to update all 2 million records at once, or is there any limit on updating the mass records? Or what is the recommended approach to do so?
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Speedforce,

May I suggest you please refer the below link to reference. hope it helps.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
speedforcespeedforce
Thank you for your response. I initiated it yesterday and in 10 hours it updated just 300k records. I wanted to check if this is quite usual, or if there is a way to make this batch update faster? I want to update 2 million records. Below is the code I am using:
 
global class UpdateFields implements Database.Batchable<SObject>{
    global String query;
    global String Entity;
    global String Email;

    global Database.QueryLocator start(Database.BatchableContext BC){
        query = 'SELECT End_Date_180_Prior__c, Pardot_End_Date_180_Prior__c, Pardot_Related_C_Account__c, AccountId, Id FROM contact';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Contact> scope){

        List<Contact> contcts = (List<Contact>)scope;

        Map<Id, Account> accts = new Map<Id, Account>([SELECT Related_C_Account_Text__c,
                Membership_Status__c
            FROM Account WHERE Id IN (SELECT AccountId FROM Contact WHERE Id = :scope)]);

        for(Contact c : contcts){

            Account a = accts.get(c.AccountId);
            
            c.Pardot_End_Date_180_Prior__c              = c.End_Date_180_Prior__c;
            //updating 10 more fields here
            
            if(a != null) {
                c.Pardot_Related_C_Account__c             = a.Related_C_Account_Text__c;
                a.Pardot_Membership_Status__c             = a.Membership_Status__c;
            }            
        }

        update contcts;
        update accts.values(); // save changes to the accounts
    }
    
    global void finish(Database.BatchableContext BC) {
        /*Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {email});
        mail.setReplyTo('user@company.org');
        mail.setSenderDisplayName('Account and Contacts Batch Processing');
        mail.setSubject('Batch Process Completed');
        mail.setPlainTextBody('Batch Process has completed');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });*/
    }
}