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.

Revisions

  1. @AlwaysThinkin AlwaysThinkin revised this gist Jun 14, 2019. 1 changed file with 15 additions and 48 deletions.
    63 changes: 15 additions & 48 deletions Batch Field Updater Test
    Original file line number Diff line number Diff line change
    @@ -2,66 +2,33 @@
    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>();

    List<Lead> newLeads = new List<Lead>();
    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);
    newLeads.add(l);
    }

    //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);
    }
    newLeads.add(new Lead(LastName = 'LastName', LeadSource = 'Web', Company = 'Some Company'));

    //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;
    insert newLeads;

    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));
    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 = '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);
    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);
    }

    }
  2. @AlwaysThinkin AlwaysThinkin revised this gist Sep 29, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions Batch Field Updater
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,14 @@
    *
    * 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));
    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,o,f,v),1);
    * Example: Id batchInstanceId1 = Database.executeBatch(new BatchRecordUpdater(q,e,f,v),1);
    */

    global class BatchFieldUpdater implements Database.Batchable<sObject>{
  3. @AlwaysThinkin AlwaysThinkin created this gist Sep 29, 2016.
    48 changes: 48 additions & 0 deletions Batch Field Updater
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    /* 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){

    }

    }
    69 changes: 69 additions & 0 deletions Batch Field Updater Test
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    @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);
    }

    }

    }