There have been countless times that we have written an Apex Trigger
or developed a workflow rule that fires on a record update, and after
completing it, I wanted to force it to fire on all records. What I used
to do was to export all of the records using the Apex Data Loader, then
do another Data Loader Update job to invoke the trigger. Sound
familiar? While this works, this is time intensive.
Batch Apex to the rescue!
Batch Apex to the rescue!
global class InvokeUpdateTriggerBatch implements Database.Batchable { private String sObjectName; global InvokeUpdateTriggerBatch(String sObjectName) { this.sObjectName = sObjectName; } global Database.QueryLocator start(Database.BatchableContext BC){ String query = 'SELECT Id FROM ' + sObjectName; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List scope){ //Just update the records. That's all! update scope; } global void finish(Database.BatchableContext BC){ System.debug('Batch Process Complete'); }} Then to run the code, open up the Debug Log and run this... InvokeUpdateTriggerBatch batch = new InvokeUpdateTriggerBatch('Lead');
Id batchId = Database.executeBatch(batch);