function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SakthidasanSakthidasan 

why we need to use Batch class  in real time?

Batch class as exposed as interface implement by developer.it is helpful for handle large number of records.Please explain real time exmple why we need to use Batch class  in real time?
Kishore B TKishore B T
For Example Lets, we assume that you have invested in a MicroFinanace and they use Salesforce as their tool.
They calculate their interest on the daily basis, that is in salesforce they handle loans using batch class for updating their interest calculation.
To avoid governor limits dev's use batch-wise processing the records.
Mahesh DMahesh D
Hi Sakthisasan,

Here I would like to ask you to go through the below post which I explain very clearly:

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBtv

Here is the realtime scenario.

If you have a business logic to send an email 180 days before the Contract expiration, we want to notify the User that the Contract is going to expire.

If we have only 180 days is the criteria then we can achieve this using Time based Workflow. But for this we need to touch the records as least once.

If we have additional criteria along with 180 days less than and also should work for all existing records as well then we will use some concept which should run the background where there is no need of user modifying the record.

In this situation we will go with Batch Apex, which will run everyday night and check all the Contracts which are expiring in next 180 days and send an email to the Contact person.


--------------------------------------------------------------------------------------------------------------------------

Please check below post for Batch job:
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html

Batch Apex
A Batch class allows you to define a single job that can be broken up into manageable chunks that will be processed separately.

When to use Batch Apex
One example is if you need to make a field update to every Account in your organization. If you have 10,001 Account records in your org, this is impossible without some way of breaking it up. So in the start() method, you define the query you're going to use in this batch context: 'select Id from Account'. Then the execute() method runs, but only receives a relatively short list of records (default 200). Within the execute(), everything runs in its own transactional context, which means almost all of the governor limits only apply to that block. Thus each time execute() is run, you are allowed 150 queries and 50,000 DML rows and so on. When that execute() is complete, a new one is instantiated with the next group of 200 Accounts, with a brand new set of governor limits. Finally the finish() method wraps up any loose ends as necessary, like sending a status email.

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

Need of Batch Apex: - As you all might know about the salesforce governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governor limits.
But for medium to large enterprises, it is essential to manage thousands of records every day. Adding/editing/deleting them when needed.
Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.
We have to create an global apex class which extends Database.Batchable Interface because of which the salesforce compiler will know, this class incorporates batch jobs. Below is a sample class which is designed to delete all the records of Account object (Lets say your organization contains more than 50 thousand records and you want to mass delete all of them).


Sample Batch Apex
1) Start method is automatically called at the beginning of the apex job. This method will collect record or objects on which the operation should be performed. These record are divided into subtasks & passes those to execute method.

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

3) Finish method executes after all batches are processed. Use this method to send confirmation email notifications.
 
global class deleteAccounts implements Database.Batchable{

global final String Query;

global deleteAccounts(String q){
Query=q;
}

global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC,List scope){
List <Account> lstAccount = new list<Account>();
for(Sobject s : scope){
 Account a = (Account)s;
lstAccount.add(a);
}
Delete lstAccount;
}

global void finish(Database.BatchableContext BC){
                //Send an email to the User after your batch completes

                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

String[] toAddresses = new String[] {‘sforce2009@gmail.com’};

mail.setToAddresses(toAddresses);

mail.setSubject('Apex Batch Job is done‘);

mail.setPlainTextBody('The batch Apex job processed ');

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

}
Please let me know if it helps you.

Regards,
Mahesh

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post.
1) http://amitsalesforce.blogspot.in/2016/02/batch-apex-in-salesforce-test-class-for.html

Batch Apex
A Batch class allows you to define a single job that can be broken up into manageable chunks that will be processed separately.


When to use Batch Apex
One example is if you need to make a field update to every Account in your organization. If you have 10,001 Account records in your org, this is impossible without some way of breaking it up. So in the start() method, you define the query you're going to use in this batch context: 'select Id from Account'. Then the execute() method runs, but only receives a relatively short list of records (default 200). Within the execute(), everything runs in its own transactional context, which means almost all of the governor limits only apply to that block. Thus each time execute() is run, you are allowed 150 queries and 50,000 DML rows and so on. When that execute() is complete, a new one is instantiated with the next group of 200 Accounts, with a brand new set of governor limits. Finally the finish() method wraps up any loose ends as necessary, like sending a status email.

Limits in Batch Apex
1) Up to five queued or active batch jobs are allowed for Apex
2) Cursor limits for different Force.com features are tracked separately. For example, you can have 50 Apex query cursors, 50 batch cursors, and 50 Visualforce cursors open at the same time.
3) A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed
4) If the start method returns a QueryLocator, the optional scope parameter of Database.executeBatch can have a maximum value of 2,000. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of up to 2,000 records. If the start method returns an iterable, the scope parameter value has no upper limit; however, if you use a very high number, you may run into other limits.
5) If no size is specified with the optional scope parameter of Database.executeBatch, Salesforce chunks the records returned by the start method into batches of 200, and then passes each batch to the execute method.Apex governor limits are reset for each execution of execute.
6) The start, execute, and finish methods can implement up to 10 callouts each
7) The maximum number of batch executions is 250,000 per 24 hours
8) Only one batch Apex job's start method can run at a time in an organization. Batch jobs that haven’t started yet remain in the queue until they're started. Note that this limit doesn’t cause any batch job to fail and execute methods of batch Apex jobs still run in parallel if more than one job is running



Need of Batch Apex: - As you all might know about the salesforce governor limits on its data. When you want to fetch thousands of records or fire DML on thousands of rows on objects it is very complex in salesforce and it does not allow you to operate on more than certain number of records which satisfies the Governor limits.
But for medium to large enterprises, it is essential to manage thousands of records every day. Adding/editing/deleting them when needed.
Salesforce has come up with a powerful concept called Batch Apex. Batch Apex allows you to handle more number of records and manipulate them by using a specific syntax.

1) When you want to process large number of records on daily basis or even on specific time of interval then you could go for Batch Apex
2) Also, when you want an operation to be asynchronous then you could implement the Batch Apex. Batch Apex is exposed as an interface that must be implemented by the developer. Batch jobs can be programmatically invoked at runtime using Apex. Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks of data


Let us know if this will help you
 
Ravi Dutt SharmaRavi Dutt Sharma
Hey Sakthidasan,

When you are procssing a large number of records, you may hit governor limits in your code. In that case, you can go for a batch class bacause a batch class will provide you a higher set of governor limits. Also in a batch class, you can control how many records you want to prcoess in a single batch. Every batch will have its own set of governor limits. For example, if the batch size of your batch job is 100 and you get 1000 records in your start method, there would be 10 batches generated and each batch will have a separate set of governor limits.
Sreenu Reddy 16Sreenu Reddy 16
I very much appreciate [ your  staff on batch apex thanks you lot kishore, mahesh D  your taken example superb