Forked from AlwaysThinkin/Batch Field Updater
          
        
    
          Last active
          September 27, 2022 09:25 
        
      - 
      
- 
        Save junaideqbal/d0c496fb8bccfa5c73b3ded5c1c70d70 to your computer and use it in GitHub Desktop. 
Revisions
- 
        junaideqbal renamed this gist Sep 27, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewingFile renamed without changes.
- 
        junaideqbal renamed this gist Sep 27, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewingFile renamed without changes.
- 
        AlwaysThinkin revised this gist Jun 14, 2019 . 1 changed file with 15 additions and 48 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> 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); } } 
- 
        AlwaysThinkin revised this gist Sep 29, 2016 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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>{ 
- 
        AlwaysThinkin created this gist Sep 29, 2016 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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){ } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } } }