In the previous section, you used the developer console to delete less than 10,000 records. Let's assume that you need to delete more than 10,000 records. You could write a simple program using Batch Apex to process up to 50 million records.
A batch
class permits you to define a single job that can be broken up into small parts that will be processed separately. You might be aware about Salesforce having governor limits on its data. When you want to fetch large numbers of records or fire DML on hundreds of rows on objects, it is very difficult in Salesforce; it does not allow you to perform on more than a certain number of records that fulfills the governor limits. For medium to large enterprises, it is necessary to process a large amount of records everyday. You can use this way to add/edit/delete Chatter posts when needed. Salesforce has come up with a great concept called Batch Apex. Batch Apex allows you to handle large numbers of records in small parts and manipulate them using a specific syntax.
For example, if you need to make a field update to every contact in your organization, and you have 10,001 contact records in your organization, it is not possible without a way of breaking it up; otherwise it will hit the governor limits. For this, you need to define the query in the start()
method that you are going to use in this batch context: select Id
from Contact
. Then the execute()
method runs, but only receives a small list of records (by default it is 200). Within the execute()
method, everything runs in a transactional context. So each time execute()
will run, you are allowed 150 DML operations, to modify 10,000 rows, and so on. When this execute()
is complete, a new one is instantiated with the next group of 200 contacts with a brand new set of governor limits. Finally, the finish()
method wraps up any loose ends as necessary, such as sending a status e-mail.
To create a Batch Apex in your organization follow these instructions:
globalDatabase.QueryLocator start(Database.BatchableContext BC) {return Database.getQueryLocator('SELECT Id FROM OpportunityFeed LIMIT 1000000'),// define the size of jobs }global void execute(Database.BatchableContext BC, List<sObject> scope)// defines size of each batch of records { delete scope; } global void finish(Database.BatchableContext BC)// you can use this for any post notification{ }
DFC batchinstanceid = database.executeBatch(new DeleteOpportunityChatter Feed(), 10000);
This sends the batch job to the Apex job queue.
System.scheduleBatch
method to schedule a batch job to be run once at a future time.3.137.151.61