+ Start a Discussion
Chad RitchieChad Ritchie 

Adding Wait/Pause/Delay Clause to Apex Code?

Hi Developer Community! 

I wrote this code in the Execute Anonymous Window, essentially it finds the first record where Trigger__c is false, and makes it True.
List<Rep_Codes__c> replist = [SELECT Id, Trigger__c FROM Rep_Codes__c Where Trigger__c = False LIMIT 1];

for(Rep_Codes__c rep :replist){
    rep.Trigger__c = True;
}

update repList;
I need to make this update for a few hundred records, but the process that's being fired will blow up when it's run on more than a few records at a time.. And unfortunately our DataLoader isn't working right now.. so I'd like to figure out a way to have this code run and once it's finished it finds the next record, or something along those lines. Is this possible in an anonymous window? Not looking to have to write a test class or anything..

Thanks!! 
Suraj Tripathi 47Suraj Tripathi 47
Hi,

You can make a batch class to update the fields
global class RepCodeBatch implements Database.Batchable<sObject>  {
   
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT Id, Trigger__c FROM Rep_Codes__c Where Trigger__c = False');
    }
    
    global void execute(Database.BatchableContext BC, List<Rep_Codes__c> replist){
        try{
            
			for(Rep_Codes__c rep :replist){
    rep.Trigger__c = True;
}

update repList;
          
        }
        
        catch(exception e){
            system.debug('Error-- '+e.getMessage()+' at Line Number-- '+e.getLineNumber());
        }
    }
    
    global void finish(Database.BatchableContext BC){
    
        system.debug('finish'); 
          
    }
}

//////////////
Run batch class like this:-
RepCodeBatch bc = new RepCodeBatch();
database.executeBatch(bc);

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

.