+ Start a Discussion
Alex MezaAlex Meza 

How can I update more than 10,000 Records?

How can I update more than 10,000 records using an apex batch class at a time?
 
v varaprasadv varaprasad
Hi alex,

using batchapex we can update upto 50million records.
please check below sample code:
 
​global class batchAccountUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name FROM Account';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         for(Account a : scope)
         {
             a.Name = a.Name + 'Updated';            
         }
         update scope;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}


here in each batch it will tke 2000 records.
 
batchAccountUpdate  bch = new batchAccountUpdate ();
database.executebatch(bch ,2000);



Using dtaloader we can update up to 5 million records.please let me know in case any help.




Thanks
Varaprasad
 
Alex MezaAlex Meza

Can I do this while I am already using a batch apex class to query more than 50,000 records, and where I am looking over two objects?

i.e. can I add this to my code below?

global class UpdateVoterFileonContacts implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = ' Select ID,First_Name__c, Last_Name__c,Zipcode__c from Voter_File_TX__c';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext info, List<Voter_File_TX__c> scope){
        Set<String> set_Str = new Set<string>();
        Map<String,Contact> mp_Cont;
 
        for(Contact Cont : [Select ID,FirstName,LastName,MailingPostalCode, Voter_File_ID__c From Contact where Contact.voter_File_ID__c != null AND Contact.RNC_ID__c != null LIMIT 50000] ){
            if(mp_Cont==null){
                mp_Cont = new Map<String,Contact>();
            }
            mp_Cont.put(Cont.FirstName +''+ Cont.LastName +''+Cont.MailingPostalCode,Cont);
        }
        for(Voter_File_TX__c VoterList : scope){
            if(mp_Cont!=null && mp_Cont.containsKey(VoterList.First_Name__c +''+ VoterList.Last_Name__c+''+ VoterList.Zipcode__c))
           
            {
                mp_Cont.get(VoterList.First_Name__c +''+ VoterList.Last_Name__c +''+ VoterList.Zipcode__c).Voter_File_ID__c = VoterList.id;
            }
        }
        /* if(mp_Cont!=null && mp_Cont.values()!=null){
            update mp_Cont.values();
        }
        */
       
        if(!mp_Cont.isEmpty()){
            update mp_Cont.values();
        }
    }
   
    global void finish(Database.BatchableContext info){
       
    }
}
Charisse de BelenCharisse de Belen
Hi Alex,

The limit for the number of SOQL queries you can execute is separate from the limit for the number of DML statements you can execute, but you cannot update more than 10,000 records in a single batch (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm).

I suggest writing a unit test to test your logic. Just keep in mind that you can only run the execute method once (ie. pass only a single batch) when writing a unit test for a batchable class.
Prakash DevloperPrakash Devloper
I am practicing batchapex code.

batchApex obj = new batchApex();
Database.executeBatch(obj,2000);

When i Run this code it is showing error 
First error: Too many DML rows: 10001

Using bath apex we can process 5 millionrecords right.Why it showing DML error.

public class batchApex implements Database.Batchable<sobject> {
    public static List<account> aclist = new List<Account>();
    public static Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('select Name From Account');
    }
    public static void execute(Database.BatchableContext bc,List<Account> accList){
        
        for(integer i=0;i<50002;i++){
            Account acc = new Account();
            acc.Name='Test'+i;
            acc.AccountNumber='1234567';
            aclist.add(acc);            
        }
        insert aclist;
    }
    public static void finish(Database.BatchableContext bc){
        
    }
    
}