+ Start a Discussion
Alex MezaAlex Meza 

Need Help writing a Batch Class to change more than 10,000 Records (DML) at a time.

I have a database with an object Voter_File_TX with over 15 million records and have over 700,000 contacts. I need to connect records that have the same First Name, Last Name, and Zipcode across these two via a look up field. 

I had to write a batch apex class that I execute anonymously beacuse of the enormous size of my database. I was told that the only way to do this is to do a batch class that will allow me to query more than 50,000 records at a time in order to match them up against each other. I was able to do this and have them connected in my sandbox where I do not have 15 million records.

However when I ran this into my production enviroment it failed because of SF limit of of 10,000 DML, or ability to change records, at a time. I was then also told that I need to write a batch class within this batch class in order to get over this limit as well. 

Can you please help me out with this I am stuck with this part. 

Below is the Apex Batch Class code. 
 
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){
        
    }
}


I was told that I need to add this type of call to my apex code but I am not sure where how this would transalte into my apex batch class code because I am already currently writting an apex batch class and because i do not know where exactly to add it. 

 

Example Code to add: 

List abc = [Select Id from Contact where Amount =100]; 
for(i=0; i<=abc.size(); i++) 
{ 
List a = abc[i]; 
a.add(abc); 
}

Can anyone please help implment this or tell me of another way wherre I can get around Salesforce govern limit of only updating 10,000 records at a time?

Thanks!
Prateek BhattPrateek Bhatt
It seems like your apex batch class is perfect. You just need to execute this batch class with apex batch size. Below are the sample code to execute the apex batch class with batch size. 
 
UpdateVoterFileonContacts UpdateVoter = new UpdateVoterFileonContacts();
Database.executeBatch(UpdateVoter, 200);

Please let me know if it doesn't work for you or any concerns.

Thanks