Skip to content

Instantly share code, notes, and snippets.

@AlwaysThinkin
Last active February 23, 2024 01:27
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AlwaysThinkin/99c01f13901ad06ab39a25e0d84c2769 to your computer and use it in GitHub Desktop.
Save AlwaysThinkin/99c01f13901ad06ab39a25e0d84c2769 to your computer and use it in GitHub Desktop.
A generic Apex Batch Update to update a field on all records for an sObject class based on the examples from the Developer Guide
/* BatchRecordUpdate will update any 1 field on any 1 object.
*
* String values must be set to Object, Field and Value to be added to Field.
*
* Query can be modified to limit records updated.
* Example below excludes records for which Degree Offering already equals the new value.
*
* All 4 strings' values must be set to operate when executed.
*
* Execute in Anonymous Apex.
*
* By Default it will run 200 records at at a time.
* CODE TO RUN IN ANONYMOUS APEX
String e = 'Lead'; // Object to be updated
String f = 'LeadSource'; // field to be updated.
String v = 'DarkWeb'; // value with which field will be populated.
String q = 'SELECT ' + f + ' FROM ' + e; // Query to which more filters can be added like: + 'AND IsConverted = false';
Id batchInstanceId1 = Database.executeBatch(new BatchFieldUpdater(q,e,f,v));
*
* Different Batch Sizes can be set by modifying the final line to add batch size after the list of arguments.
* Example: Id batchInstanceId1 = Database.executeBatch(new BatchRecordUpdater(q,e,f,v),1);
*/
global class BatchFieldUpdater implements Database.Batchable<sObject>{
global final String Query;
global final String Entity;
global final String Field;
global final String Value;
global BatchFieldUpdater(String q, String e, String f, String v){
Query=q; Entity=e; Field=f;Value=v;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,
List<sObject> scope){
for(Sobject s : scope){s.put(Field,Value);
} update scope;
}
global void finish(Database.BatchableContext BC){
}
}
@isTest
public class BatchFieldUpdaterTest {
public static testMethod void testLead(){
Integer count = 200;
List<Lead> newLeads = new List<Lead>();
for(integer i = 0 ; i < count ; i++){
Lead l = new Lead(LastName = 'ln' + i, Company = 'Company' + i);
newLeads.add(l);
}
//Make a Lead record that will not get changed
newLeads.add(new Lead(LastName = 'LastName', LeadSource = 'Web', Company = 'Some Company'));
insert newLeads;
Test.StartTest();
String e = 'Lead';
String f = 'LeadSource';
String v = 'Phone';
String q = 'SELECT LeadSource FROM ' + e + ' WHERE ' + f + ' != \'Web\'';
Id batchInstanceId0 = Database.executeBatch(new BatchFieldUpdater(q,e,f,v));
Test.stopTest();
System.assertEquals(1, [Select count() from Lead where LeadSource = 'Web']);
List<Lead> changedLeads = [Select ID, LeadSource from Lead where LeadSource != 'Web'];
System.assertEquals(count, changedLeads.size());
for(Lead ld : changedLeads){
System.assertEquals('Phone', ld.LeadSource);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment