Using a scheduled Batch Apex to clean the Chatter data

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.

Note

To learn more about Batch Apex, go through the Salesforce online help.

To create a Batch Apex in your organization follow these instructions:

  1. Navigate to Name | Setup | App Setup | Develop | Apex Class, then click on the New button. The following is the code snippet:
    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{
    }
  2. Click on Save once you are done with the changes.
  3. To execute this class, open the developer console.
  4. In the upper-left corner, click on Debug | Open Execute Anonymous Window.
    Using a scheduled Batch Apex to clean the Chatter data
  5. Paste the following code in this window:
    DFC batchinstanceid = database.executeBatch(new DeleteOpportunityChatter Feed(), 10000);

    This sends the batch job to the Apex job queue.

  6. Now navigate to Name | Setup | Administration Setup | Monitoring | Apex Jobs, and see the progress of your job.
  7. You can use the System.scheduleBatch method to schedule a batch job to be run once at a future time.

    Note

    You can only have five queued or active batch jobs at one time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.151.61