+ Start a Discussion
DevelopersDevelopers 

How to work with Batch Apex?

Hi Friends,

 

          I am new to work with Batch apex. As per my requirement, i have to create one Batch apex class to process the 25000 records along the with some business validations. For this, i have some questions, which are mentioned below. Pls suggest me the way to use this.

 

1. Can we call another class in our Batch Apex class execute method()?

2. What is the maximum records size, we can set for the scope?(no.of records per batch)

3. upto how many records, the Batch Apex can work?

4.How to execute this Batch Apex? Types of executing the Batch Apex?

5. Can we call Batch Apex class in Trigger,Schedule Apex Class and VF Page?If yes, How to use?

 

   Pls provide me the information, even though i am studying the document of Batch Apex which is available from developers.salesforce.com

 

Regards,

Phanikumar

 

 

Best Answer chosen by Admin (Salesforce Developers) 
UVUV

Great..Pls mark the post as a solution for the other's benifit.

All Answers

UVUV

Here are answers of your questions-

1-Yes you can call the apex class from your batch apex.

2-Maximum batch size is 200.

3-Batch apex will work for the millions of records.

4-You can execute the batch apex using database.executeBatch(new Sample());

5-Yes you can call the batch apex from trigger, schedule apex class and VF pages.

calling would be done in the same way ..

 

Let me know if you have further queries.

RockersRockers

Hi Umesh,

 

      Thank you very much for valuable response. I need to know that if we can call the Batch Apex from Trigger, then how many Batch Apex Classes can we call at a time.

 

      I used to call another calss in my batch apex class which contains Business rules validations to parse the .csv file. It contains upto 25k records per week. so, if i set the batch to 200 records, then there are 125 batches will create. In that, if any one batch is failed during execution, then how could we find and resolve that.

 

      Pls help me in this. Once again, thank you very much for your reply.

     

   

Regards,

Phanikumar

UVUV

As this is a asynchronus process so would be great if you call less number of jobs from your trigger since there can be 5 active jobs only.If any job is failed then you can see the status of jobs under "Apex Jobs"...

 

Keep in mind the following governor limits for batch Apex:

  • Up to five queued or active batch jobs are allowed for Apex.
  • A user can have up to five query cursors open at a time. For example, if five cursors are open and a client application still logged in as the same user attempts to open a new one, the oldest of the five cursors is released.

    Cursor limits for different Force.com features are tracked separately. For example, you can have five Apex query cursors, five batch cursors, and five Visualforce cursors open at the same time.

  • A maximum of 50 million records can be returned in theDatabase.QueryLocatorobject. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.
  • The maximum value for the optional scope parameter is 400. If set to a higher value, Salesforce chunks the records returned by the QueryLocator into smaller batches of 400.
  • If no size is specified with the optional scope parameter, Salesforcechunks the records returned by the QueryLocator into batches of 200, and then passes each batch to theexecutemethod. Apexgovernor limits are reset for each execution ofexecute.
  • Thestart,executeandfinishmethods can implement only one callout in each method.
  • Batch executions are limited to one callout per execution.
  • The maximum number of batch executions is 250,000 per 24 hours.
  • Only one batch Apexjob'sstartmethod 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 andexecutemethods of batch Apex jobs still run in parallel if more than one job is running.
RockersRockers

Hi Umesh,

 

         I am very happy for knowing the things about BatchApex. Thank you very much.

 

      Could you please explain, how can we call an apex class in our Batch Apex Job and how can we execute this Batch Apex job?

 

        Explain me briefly with an example, if possible for the above query......

 

       Pls provide me the information. Once again,thank you very much.

 

Regards,

Phanikumar

UVUV

Suppose "Sample" is your batch class and you want to execute it from the trigger then-

1- Define a public method in your batch job.for example

public void RunBatch()

{

        database.executeBatch(new Sample());// pass the optional parameters if you want to pass

}

 

2- call this public method from your trigger like-

new Sample().RunBatch();

 

Or

 

you can directly call the batch apex from your trigger in this way-

        database.executeBatch(new Sample());// pass the optional parameters if you want to pass

 

Furthermore, if you want to call the batch apex fromt he schedulable class then use database.executeBatch().

 

Let me know if this information is good enough for you to go ahead for batch job creatiion and execution.

RockersRockers

Hi Umesh,

 

              Yes, i got the required information. Thank you very much. Now, i have little bit doubt about the execution of Batch Class.

 

          I created one batch apex class names as "MyBatchApex" and implemented the Database.Batchable<sObject>.

         Now, i need to define the scope value which is used in execute method. How can i give the scope value at runtime.

By using Database.executeBatch(new MyBatchApex) method, the execute method will execute automatically. Or, we need to call by using class Object of Batch Apex Class.

 

global void execute(Database.BatchableContext BC, List<sObject> scope)

         So, pls provide me the information. Thanks for every thing. Now, i am getting clear idea about Batch Apex.

 

Regards,

Phanikumar

    

UVUV

Hi,

 

You need to use to database.executemethod(class object) to execute the batch...There is a sequence of execution defined for batch class so you dont need to bother about anything.

I would suggest you to go thorugh it-

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

RockersRockers

Hi Umesh,

 

         i am also studying that. I am very happy with your support. I get the clear idea about this.

 

        Now iam trying some excercises. If i have any queries, i will get back to you.

 

        Once again, Thank you very much for co-operation. :manvery-happy:

 

Regards,

Phanikumar

UVUV

Great..Pls mark the post as a solution for the other's benifit.

This was selected as the best answer
RockersRockers

Hi Umesh,

 

       I tried to work with Batch Apex on an custom object which contains 20000 records. Also, i implemented some conditional checking in the execute method. Now, i want to calculate the completion time by a Batch Apex while processing such a huge data.

 

       At the time of processing, it took much time. But, ater completion, it shows the next to the started time.

 

       Is there any way to calculate the processing time for a Batch Apex Class.

 

Regards,

Phanikumar

UVUV

As this is asynchronus process and depends on the resouces available I dont think we can get each and every information. You can only see the status from here-

https://login.salesforce.com/apexpages/setup/listAsyncApexJobs.apexp?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DMonitoring&setupid=AsyncApexJobs

When batch process will take say few hrs to complete then you would be able to see the difference.

You are free to log a case with salesforce to get the exact time details.