+ Start a Discussion
Alejandro RosanoAlejandro Rosano 

Help using batch apex

Hello,
 

I'm quite new in Apex and no experience at all with batches so any kind of help would be great.

I have the standard object User, with two custom fields User.FA__c (text) and User.FB__c (checkbox).

I need to recover the field FA__c for all the users in the org that have the field FB__c = true.

I've been reading about Using Batch Apex but still not so clear about how to use the start, execute and finish methods.

Thanks in advance and kind regards.
Best Answer chosen by Alejandro Rosano
ManojjenaManojjena
Hey Alejandro,
The class which will implement Batchable interface in SFDC term we were aclling that class as batch class .
Bacically to avoid limits in salesforce we are using batch apex .

There are two interface in DataBase Namespace

1.Batchable Interface

Public System.Iterable start(Database.BatchabelContext bcon)

Public Database.QueryLocator start(Database.BatchabelContext bcon)

Public void execute(Database.BatchableContext bc,List<Sobject> scope)

Public void finish(Database.BatchableContext bc)
 
2.BatchableContext Interface

 Public Id getJobId()
 public Id getChildJobId()

Basic thing is that once you will implement Batchable interface in your class you have to implement three methos in that interface .
Flow is that when you execute your class start method will execute once and return a list in two different form as we have two types of return type in start method .

The list which will return from the start method will available in execte methos scope .exeute method will execute according to your batch size .
Assume that you have 1000 record ,if you set your batch size as 100 then your execute method will execute 10 times .
After finishing the execution your finish method will execute once .

Basically in the signature of all method one parameter we are passing named BatchabeleContext ,this is to get the batchJob Id and child Job Id .
BatchJodid is nothing but there is an object in salesforce called "AsyncApexJob" where salesforce is creating a record for each batch job .
How to exceute the batch apex .

IN Database class there are two static method
executeBatch(Instance Of class )
excecuteBatch(InstanceOf batch class ,batchsize )

If you want to defind any linit of record to pass into execute method per execution then you need to use second method .
If you will call first method then it will take the default batch size as 200.

For small example you can use this below link .

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

Any doubt let me know .

If you think this is help full then select this as best answer to help others .
 

All Answers

NagaNaga (Salesforce Developers) 
Dear Alejandro,

1. To use batch Apex in Salesforce, you have to write an Apex class that implements the Database.Batchable interface. Then invoke the Apex class pro-grammatically.

2. The Database.Batchable interface has three methods that need to be implemented:

A. Start
B. Execute
C. Finish

 

3. Every execution of a batch Apex is a discrete transaction.

 

4. Use the Database.BatchableContext object to track the progress of the batch job.

 

5. All methods in the Database.Batchable interface require a reference to a Database.BatchableContext object. You can only test one execution of the execute method.

 

6. Some important Batch Apex governor limits include:

A. Up to five queued or active batch jobs

B. Batch Apexstart method can have up to 15 query cursors per user open at a time.

C. Batch Apex execute and finish methods have a limit of 5 open query cursors per user.

D. The start, execute, and finish methods can implement up to 10 callouts each.

 

7. When you test batch Apex, you must make certain that the batch job is finished using methods startTest and stopTest around the executeBatch method.

 

8. Methods declared as future can’t be called from a batch Apex class and aren’t allowed in classes that implement the Database.Batchable interface.

 

9. Each time batch Apex is invoked, it creates an AsyncApexJob record. You can use the ID of this record to construct a SOQL query to retrieve the job’s status and progress.

 
10. Apex creates one additional AsyncApexJob record of type BatchApexWorker for internal use for every 10,000 AsyncApexJob records.


Please let me if this information helps you.

Best Regards
Naga Kiran
ManojjenaManojjena
Hey Alejandro,
The class which will implement Batchable interface in SFDC term we were aclling that class as batch class .
Bacically to avoid limits in salesforce we are using batch apex .

There are two interface in DataBase Namespace

1.Batchable Interface

Public System.Iterable start(Database.BatchabelContext bcon)

Public Database.QueryLocator start(Database.BatchabelContext bcon)

Public void execute(Database.BatchableContext bc,List<Sobject> scope)

Public void finish(Database.BatchableContext bc)
 
2.BatchableContext Interface

 Public Id getJobId()
 public Id getChildJobId()

Basic thing is that once you will implement Batchable interface in your class you have to implement three methos in that interface .
Flow is that when you execute your class start method will execute once and return a list in two different form as we have two types of return type in start method .

The list which will return from the start method will available in execte methos scope .exeute method will execute according to your batch size .
Assume that you have 1000 record ,if you set your batch size as 100 then your execute method will execute 10 times .
After finishing the execution your finish method will execute once .

Basically in the signature of all method one parameter we are passing named BatchabeleContext ,this is to get the batchJob Id and child Job Id .
BatchJodid is nothing but there is an object in salesforce called "AsyncApexJob" where salesforce is creating a record for each batch job .
How to exceute the batch apex .

IN Database class there are two static method
executeBatch(Instance Of class )
excecuteBatch(InstanceOf batch class ,batchsize )

If you want to defind any linit of record to pass into execute method per execution then you need to use second method .
If you will call first method then it will take the default batch size as 200.

For small example you can use this below link .

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm

Any doubt let me know .

If you think this is help full then select this as best answer to help others .
 
This was selected as the best answer