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
rajaram.adminrajaram.admin 

what is batch apex?explain one example.

HI,

 

 

 

 

Thanks & Regards

Rajaram.

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

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).

 

Examples:-

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 });

}

}

 

//This is how the batch class is called.

id batchinstanceid = database.executeBatch(new deleteAccounts(‘select Id from Account’))

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

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).

 

Examples:-

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 });

}

}

 

//This is how the batch class is called.

id batchinstanceid = database.executeBatch(new deleteAccounts(‘select Id from Account’))

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
Vamsi:)Vamsi:)
Very Clear explaination Navatar...Thanq man...:)
Sandesh D GanjareSandesh D Ganjare
Hi,
I want to only delete records having Account Name = "Xyz prt lim."  Where to write the quary for that.
Waiting for Reply...!

Thanks,
Sandesh
Sumeet_ForceSumeet_Force
hi sandesh...
Check the query in the last line where batch class is being called. The SOQL can be modified by appending a where clause to fetch specific records from the object.

Regards,
Satya sai sudhirSatya sai sudhir
How do we add 100 account records using batch apex? 
belabela
Hi navatar,

should take only global as access modifiers for batche classes and methods?if not please what access modifiers we can use for batch method and classes

Thanks ,
​navatar
Akshay KopardeAkshay Koparde
Hi Rajaram.Admin,

Let me give a example for batchable apex for better understanding.
Assume that you are 20 friends and are going out for dinner. Assume you have only 1 car which can accomodate 4 members from your hostel to the restaurant.  So to move all the 20 members from hostel to restaurant the car has to travel 5 times between the hostel and restaurant. With this the batch size would be 4. This batch size in salesforce  refers to governor limits. Hence the multiple records exceeding the Governer limits are split into batches so that we dont override the governer limits. 

My explaination is only to make you understand the concept of Batchable apex. Example code has been already mentioned by others here.

I hope I have conveyed the best explaination for batch apex to my knowledge. 
 
sunil chandelsunil chandel
no need of query variable if u dnt want u cn direct use query in return database.getquerylocator(select id ,name,..... frrm __ if u want)
farukh sk hdfarukh sk hd
Here is good information covered on batch class in salesforce,

https://www.sfdc-lightning.com/2018/09/batch-class-in-salesforce.html
NILESH RAJ 20NILESH RAJ 20
Batch apex covers the entire set of records that needs to be processed and divide it into small manageable chunks. This is a very powerful method of executing millions of records like in data cleaning or archiving records which otherwise would hit the governor limit in normal DML operation.
You must check https://sfdcwisdom.com/batch-apex-in-salesforce/ (https://sfdcwisdom.com/batch-apex-in-salesforce) for complete detail on batch apex.