How To Process Large Records in Salesforce Without Hitting the Governor Limits

Batch Classes: -

Batch Apex is exposed as an interface that must be implemented by the Salesforce developer. Batch jobs can be programmatically invoked at the runtime using Salesforce Apex.

Why to use Batch Apex: - As we all are updated with the Salesforce governor limits on its data limit. When we want to fetch thousands or huge records or fire some DML operation on thousands of rows on objects it is very complex in Salesforce as it does not allow us to operate more than certain number of records which keep us in the Governor limits.

But for medium to large firms, it is mandatory to manage thousands/huge number of records every day. Performing Adding/editing/deleting operations on the records when needed.

Salesforce has come up with a strong concept called Batch Apex. Batch Apex allows you to handle more number of records and change them by using a certain implementation.

To create a batch class we have to create a global apex class which implements the Database.Batchable Interface. Using this Interface the Salesforce compiler will get to know, this class contains batch jobs. Below is a sample class with the example which is developed to delete all the records of Account object in Salesforce (Lets say your Salesforce org contains more than 50 thousand records and you want to mass delete all of them).

The Database.Batchable interface has three methods. These are:
1. Start
2. execute
3. Finish

Start method is always called at the beginning of the Apex Batch. This method will collect all the record or objects on which the operation has to be performed. These record from the start are divided into subtasks & passed to the execute method.

Execute Method performs the operation which we want to perform on the records passed from the start method.

Finish method is executed after all the batches are processed. This method is mostly used to send the confirmation email notifications.

Example:-

global class sampleTest implements Database.Batchable{
    public final String Query;
    public sampleTest(String account){
        Query=account;
    }
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC,List scope){
        List listAccounts = new list();
        for(Sobject sc : scope){
            Account aglobal class sampleTest implements Database.Batchable{<br ></br >cc = (Account)sc;
            listAccounts.add(acc);
        }
        Delete listAccounts;
    }
    global void finish(Database.BatchableContext BC){
        //Send an email to the User after your batch completes
        Messaging.SingleEmailMessage mailSend = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'[email protected]'};
        mailSend.setToAddresses(toAddresses);
        mailSend.setSubject('Apex Batch Job has been done.');
        mailSend.setPlainTextBody('The batch Apex job has processed.');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mailSend });
    }
}

//This is how the batch class is called.
Id batchinstanceAccountid = database.executeBatch(new sampleTest(‘select Id,name from Account’))

 

Responses

Comments are closed.

Popular Salesforce Blogs

Salesforce Marketing Cloud Integration with Google Analytics 360

Redefining the future of Marketing – Salesforce Marketing Cloud Integration with Google Analytics 360

Blog in

Know your customers better and make smarter, insight-based marketing decisions through Salesforce’s massive customer relationship management platform twinned with Google Analytics 360’s market-leading digital analytics solution…