Skip to content

Instantly share code, notes, and snippets.

@wsaribeiro
Forked from AlwaysThinkin/Batch Field Updater
Created February 23, 2024 01:27
Show Gist options
  • Save wsaribeiro/d2cb6f1308b999f3cb747ef0c252d70b to your computer and use it in GitHub Desktop.
Save wsaribeiro/d2cb6f1308b999f3cb747ef0c252d70b 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 Opportunity or Application_Item__c
String f = 'Degree_Offering__c'; // field to be updated.
String v = 'do-umt'; // value with which field will be populated.
String q = 'SELECT ' + f + ' FROM ' + o + ' WHERE ' + f + ' != ' + v; // Query to which more filters can be added like: + 'AND IsConverted = false';
Id batchInstanceId1 = Database.executeBatch(new BatchRecordUpdater(q,o,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,o,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> lds0 = new List<Lead>();
List<Account> accts0 = new List<Account>();
List<Opportunity> opps0 = new List<Opportunity>();
for(integer i = 0 ; i < count ; i++){
Lead l = new Lead(LastName = 'ln' + i, Company = 'Company' + i);
lds0.add(l);
Account a = new Account(Name = 'name' + i);
accts0.add(a);
}
//Make a Lead record that will not get changed
lds0.add(new Lead(LastName = 'scaba', LeadSource = 'sc-aba', Company = 'SC-ABA'));
insert lds0;
insert accts0;
for(integer i = 0 ; i < accts0.size() ; i++){
Opportunity o = new Opportunity(Name = 'name' + i, AccountID = accts0[i].Id,
StageName = 'Started Application', CloseDate = system.today());
opps0.add(o);
}
//Make an Opportunity record that will not get changed
opps0.add(new Opportunity(Name = 'scaba', LeadSource = 'sc-aba',
StageName = 'Started Application',CloseDate = system.today()));
insert opps0;
Test.StartTest();
String e = 'Lead';
String f = 'LeadSource';
String v = 'sc-mba';
String q = 'SELECT LeadSource FROM ' + e + ' WHERE ' + f + ' != \'sc-aba\'';
Id batchInstanceId0 = Database.executeBatch(new BatchFieldUpdater(q,e,f,v));
e = 'Opportunity';
f = 'LeadSource';
v = 'sc-mba';
q = 'SELECT LeadSource FROM ' + e + ' WHERE ' + f + ' != \'sc-aba\'';
Id batchInstanceId1 = Database.executeBatch(new BatchFieldUpdater(q,e,f,v));
Test.stopTest();
System.assertEquals(1, [Select count() from Lead where LeadSource = 'sc-aba']);
List<Lead> lds2 = [Select ID, LeadSource from Lead where LeadSource != 'sc-aba'];
System.assertEquals(count, lds2.size());
for(Lead ld : lds2){
System.assertEquals('sc-mba', ld.LeadSource);
}
System.assertEquals(1, [Select count() from Opportunity where LeadSource = 'sc-aba']);
List<Opportunity> opps2 = [Select ID, LeadSource from Opportunity where LeadSource != 'sc-aba'];
System.assertEquals(count, opps2.size());
for(Opportunity opp : opps2){
System.assertEquals('sc-mba', opp.LeadSource);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment