+ Start a Discussion
Alex MezaAlex Meza 

Updating more than 10,000 Records

I need help writting a batch class that queries more than 50,000 records and updates more than 10,000 records. I already was able to solve the query issue by writting the batch class below but now I need to update it so that it can update more than 10,000 records at a time. 

Just to give you a basic rundown: I am trying to connect to records across two objects, 1. Contacts and 2. Voter_File_TX__c, via a lookup field that exists Voter_File_TX__c custom object of Voter_File_TX__c.Contact__c.
 
Records across these two objects should be connected on a one to one basis if they have matching First Name, Last Name, and Zipcodes.
 
For the Apex class code of UpdateVoterFileonContacts I am able to have this complete perfectly in My Sandbox where I do not have as many records as my production.
 
However once I upload this code into my production I run into an issue because the Contact object has around 700,000 records while the Voter File Object has around 15 Million records, thus I run into Salesforce Govern Apex Limits. I was able to use the batch class to go over the 50,000 query limit and have it expanded to 50,000,000 Limit.
 
But now I am running into a DML govern limit where I can only update 10,000 Records at a time, when I should be updating around 250,000 records.
 
Because of my business logic I cannot further limit and make the query any more selective than I already have, I need to know if there is a way around this, just like there was a way around the query limit of 50,000 records.
 
Please let me know I have also included the class I am working with 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){
       
    }
}



The way I am running this class is executing in the developers console and running it anonymously. The end result if this code does work then the Voter_File__TX__c.Contact__C field ought to be populated with the corresponding contact match. 
 
As I said this works in my sandbox but fails in production because of the massive size of my records.
 
Let me know if you have any questions or need anything else.