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
sales4s.comsales4s.com 

How to write Apex Batch classes.

Hi all,

I am newbie to apex Batch classes. I need to understand how Apex batches execute(the process).

I know how to write apex classes and trigeers. but totally confused in following Apex batch code. Kindly give me some tips/knowledge/information on how easily I can write apex batches.

==========================================================

  1. global class Beautification's implements Database.Batchable<sObject>{
  2. global final String Query;
  3. global final String Entity;
  4. global final String Field;
  5. global final String Value;
  6. global UpdateAccountFields(String q, String e, String f, String v){
  7. Query=q; Entity=e; Field=f;Value=v;
  8. }
  9. global Database.QueryLocator start(Database.BatchableContext BC){
  10. return Database.getQueryLocator(query);
  11. }
  12. global void execute(Database.BatchableContext BC,
  13. List<sObject> scope){
  14. for(Sobject s : scope){s.put(Field,Value);
  15. } update scope;
  16. }
  17. global void finish(Database.BatchableContext BC){
  18. }
  19. }

The following code can be used to call the above class:

 

1. Id batchInstanceId = Database.executeBatch(new UpdateInvoiceFields(q,e,f,v), 5);

=============================================================

I am little confused of the following lines:

 

1. ""query","entity","field","value"" declared as variable and at 7th line why it is assigned to q,e,f,v.

2.  return Database.getQueryLocator(query)...?

3.  [List<sObject> scope)  {  for(Sobject s : scope){s.put(Field,Value);]..in line 13,14.

Can't we use directly Account,lead,contact or any custom object. Like this:[ List<account>scope { for(account a:scope...].

4. Id batchInstanceId = Database.executeBatch(new UpdateInvoiceFields(q,e,f,v), 5);

 

can please explain line by line.

thank you in advance.

any kind of explanation/suggestions/information is highly appriciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
Niket SFNiket SF

Hello Friend ,
Actualy batch class we use when we need to work on the bulk data so the governer limits can avoided but not 100 %.
Let me explain code line by line ...
1. "query","entity","field","value"" declared as variable and at 7th line why it is assigned to q,e,f,v.
2. return Database.getQueryLocator(query)...?


for the line number one it's not nessary that to provide all these parameters acually this only example for the you just need to provide the query to the class constructor. Here if you look carefully it returns querylocator it's used to baupass governer limit for number of records returns.

[List<sObject> scope) { for(Sobject s : scope){s.put(Field,Value);]..

In the execute method all the code is generic so when actual batch is get processed it will consider only Sobject then we need to specified Object type explicitly.
because we are provided the query in the form of string.... so you need to use Sobject.


Thanks
Nik's

Skype Name : niket.chandane

 

All Answers

Niket SFNiket SF

Hello Friend ,
Actualy batch class we use when we need to work on the bulk data so the governer limits can avoided but not 100 %.
Let me explain code line by line ...
1. "query","entity","field","value"" declared as variable and at 7th line why it is assigned to q,e,f,v.
2. return Database.getQueryLocator(query)...?


for the line number one it's not nessary that to provide all these parameters acually this only example for the you just need to provide the query to the class constructor. Here if you look carefully it returns querylocator it's used to baupass governer limit for number of records returns.

[List<sObject> scope) { for(Sobject s : scope){s.put(Field,Value);]..

In the execute method all the code is generic so when actual batch is get processed it will consider only Sobject then we need to specified Object type explicitly.
because we are provided the query in the form of string.... so you need to use Sobject.


Thanks
Nik's

Skype Name : niket.chandane

 

This was selected as the best answer
sales4s.comsales4s.com

Hi Nik, Thanks for nice explanation. And what about last line.

4. Id batchInstanceId = Database.executeBatch(new UpdateInvoiceFields(q,e,f,v), 5);

 

what purpose this line written for?

Niket SFNiket SF

Hello Friend,

 

      Id batchInstanceId = Database.executeBatch(new UpdateInvoiceFields(q,e,f,v), 5);

 

 In this method q,e,f,v these are the parameters of the class but that number "5" is nothing but the batch size means if you are going to insert 15 records at a time it will consider as 3 batches of sie 5. If we check carefully it will returns the Id that is nothing but the JOB id of that Batch size. There is one object called "CronTrigger" it will hold the information about each job.

 

 

Thank you 

Nik's

SunnyShinySunnyShiny

This topic is interesting.

I don't understand the q, its clear its a query paramter but how should i know what is popuplated inside,

is it select all from account ?