public String CloudThoughts{ get; set;}
global class BatchVerbNoun implements Database.Batchable<sObject>{ global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); //May return up to 50 Million records } global void execute(Database.BatchableContext BC, List<sObject> scope){ //Batch gets broken down into several smaller chunks //This method gets called for each chunk of work, passing in the scope of records to be processed } global void finish(Database.BatchableContext BC){ //This method gets called once when the entire batch is finished } }
BatchUpdateFoo batch = new BatchUpdateFoo(); Database.executeBatch(batch, 200);
/* Run this batch from Execute Anonymous tab in Eclipse Force IDE or System Log using the following string query = 'select Id, CompanyName from User'; BatchUpdateField batch = new BatchUpdateField(query, 'CompanyName', 'Bedrock Quarry'); Database.executeBatch(batch, 100); //Make sure to execute in batch sizes of 100 to avoid DML limit error */ global class BatchUpdateField implements Database.Batchable<sObject>{ global final String Query; global final String Field; global final String Value; global BatchUpdateField(String q, String f, String v){ Query = q; 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){ AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id = :BC.getJobId()]; string message = 'The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.'; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {a.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('Salesforce BatchUpdateField ' + a.Status); mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } public static testMethod void tests(){ Test.startTest(); string query = 'select Id, CompanyName from User'; BatchUpdateField batch = new BatchUpdateField(query, 'CompanyName', 'Bedrock Quarry'); Database.executeBatch(batch, 100); Test.stopTest(); } }
/* Run this batch from Execute Anonymous tab in Eclipse Force IDE or System Log using the following string query = 'select Id, Company from Lead'; BatchSearchReplace batch = new BatchSearchReplace(query, 'Company', 'Sun', 'Oracle'); Database.executeBatch(batch, 100); //Make sure to execute in batch sizes of 100 to avoid DML limit error */ global class BatchSearchReplace implements Database.Batchable<sObject>{ global final String Query; global final String Field; global final String SearchValue; global final String ReplaceValue; global BatchSearchReplace(String q, String f, String sValue, String rValue){ Query = q; Field = f; SearchValue = sValue; ReplaceValue = rValue; } global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject&> scope){ for(sobject s : scope){ string currentValue = String.valueof( s.get(Field) ); if(currentValue != null && currentValue == SearchValue){ s.put(Field, ReplaceValue); } } update scope; } global void finish(Database.BatchableContext BC){ AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id = :BC.getJobId()]; string message = 'The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.'; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {a.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('Salesforce BatchSearchReplace ' + a.Status); mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } public static testMethod void tests(){ Test.startTest(); string query = 'select Id, Company from Lead'; BatchSearchReplace batch = new BatchSearchReplace(query, 'Company', 'Foo', 'Bar'); Database.executeBatch(batch, 100); Test.stopTest(); } }
/* Run this batch from Execute Anonymous tab in Eclipse Force IDE or System Log using the following string query = 'select Id from ObjectName where field=criteria'; BatchRecordDelete batch = new BatchRecordDelete(query); Database.executeBatch(batch, 200); //Make sure to execute in batch sizes of 200 to avoid DML limit error */ global class BatchRecordDelete implements Database.Batchable<sObject>{ global final String Query; global BatchRecordDelete(String q){ Query = q; } global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject&> scope){ delete scope; } global void finish(Database.BatchableContext BC){ AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id = :BC.getJobId()]; string message = 'The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.'; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {a.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('Salesforce BatchRecordDelete ' + a.Status); mail.setPlainTextBody('The batch Apex job processed ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } public static testMethod void tests(){ Test.startTest(); string query = 'select Id, CompanyName from User where CompanyName="foo"'; BatchRecordDelete batch = new BatchRecordDelete(query); Database.executeBatch(batch, 100); Test.stopTest(); } }