batch apex in salesforce

28
Batch Apex Dave Helgerson davehelgerson.com @davehelgerson

Upload: david-helgerson

Post on 17-Aug-2015

115 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Batch Apex in Salesforce

Batch Apex

Dave Helgersondavehelgerson.com @davehelgerson

Page 2: Batch Apex in Salesforce

Batch Apex Agenda

• High level description of Batch Apex• How to use Batch Apex• Simple Code Examples• Demos in a Salesforce org• Questions

Page 3: Batch Apex in Salesforce

What is Batch Apex?

• Batch Apex is a way to process millions of records on the Salesforce platform

Page 4: Batch Apex in Salesforce

How does Batch Apex work?

• Start by defining a dataset to process through– Database.QueryLocator – Custom Iterator– List Collection

Page 5: Batch Apex in Salesforce

How does Batch Apex work? (cont.)

• Then, the dataset is divided into smaller chunks of records– Each chunk of dataset processes as a separate

transaction– Size of each chunk is defined when submitting the

Batch process

Page 6: Batch Apex in Salesforce

How does Batch Apex work? (cont.)

• Finally, after all chunks of the dataset have been processed, post processing can be run– Send Emails– Update aggregate totals

Page 7: Batch Apex in Salesforce

How does Batch Apex work? (cont.)

• Define the dataset

• Divide and process

• Post processing

Page 8: Batch Apex in Salesforce

How does Batch Apex run?

• Asynchronously, of course!

• Like @future methods or Queueable Apex

Page 9: Batch Apex in Salesforce

What conditions are ideal for Batch Apex?

• Tasks that involve large datasets or are processing intensive

• Source of data is from a single database object, and Dataset can be retrieved with a single SOQL statement

• Each unit of work is independent• Not time-critical

Page 10: Batch Apex in Salesforce

Give me scenarios for Batch Apex processing

• Record Ownership Reassignment• Data Retention • Data Cleansing • Recalculating Apex Managed Sharing • Other Mass Record Update

Page 11: Batch Apex in Salesforce

What does the code look like?

• Class definition implements the Database.Batchable interface

• Three methods are required– start()– execute()– finally()

Page 12: Batch Apex in Salesforce

What does the code look like? (cont.)

• QueryLocator Example

Page 13: Batch Apex in Salesforce

What does the code look like? (cont.)

• List Example

Page 14: Batch Apex in Salesforce

What does the code look like? (cont.)

• Iterable Example

Page 15: Batch Apex in Salesforce

What does the code look like? (cont.)

• Additional Class Attributes– Database.AllowsCallouts• 100 callouts per transaction (Winter ’15)

– Database.Stateful• State is maintained between transactions• Instance member variables retain their values• Static member variables are reset

– Scheduable• Combine the scheduling class with the Batch Apex class• global void execute(SchedulableContext sc){

Page 16: Batch Apex in Salesforce

How do I run a job?

• Batch Apex is invoked programmatically with Database.executeBatch or System.scheduleBatch– Visualforce page controller– Apex trigger– Ajax Toolkit– Execute Anonymous from Force.com IDE or

Developer Console– Scheduled Job

Page 17: Batch Apex in Salesforce

How do I run a job? (cont.)

• Database.executeBatch– Submits a job to the queue– Parameters: • Instance of the class• Scope (optional)

– Example:• Database.executeBatch(new ExampleBatch(), 100);

Page 18: Batch Apex in Salesforce

How do I run a job? (cont.)

• System.scheduleBatch– Creates a scheduled job– Parameters:

• Instance of the class• Job name• Time interval, in minutes, after which the job should start

executing• Scope (optional)

– Example:• System.scheduleBatch(new ExampleBatch(), 'Test', 5, 100);

Page 19: Batch Apex in Salesforce

How do I run a job? (cont.)

• Job is submitted to the queue– 5 jobs can be processing at once– 100 jobs can be in the queue at once.• Flex Queue (Spring ‘15)• Enable in the “Apex Flex Queue” in Critical Updates

• Job Id is returned– Track progress by the job Id

Page 20: Batch Apex in Salesforce

How do I schedule a job?

• Scheduling from the UI– Setup -> Develop -> Apex Classes -> Schedule Apex button

Page 21: Batch Apex in Salesforce

How do I schedule a job? (cont.)

• Scheduling in Apex • system.schedule()• Name• Cron expression• Instance of Schedulable class

• Example:– system.schedule('My Scheduled Job', '0 15 * * * ?', new ExampleSchedulable());

Page 22: Batch Apex in Salesforce

How do I monitor a job?

• Setup -> Monitor -> Jobs -> Apex Jobs

• Shows active and completed job information

Page 23: Batch Apex in Salesforce

How do I monitor a job? (cont.)

• AsyncApexJob– Accessible in Apex

Page 24: Batch Apex in Salesforce

How do I test Batch Apex?

• Use Test.startTest() and Test.endTest()• Asynchronous processes run synchronously

when the Test.endTest() command executes• The execute() method may only be invoked

once

Page 25: Batch Apex in Salesforce

How do I test Batch Apex?

Page 26: Batch Apex in Salesforce

Considerations Summary

• QueryLocator - maximum of 50m records• Custom Iterators - maximum of 50k records• Governor limits apply to each invocation of

the execute method()• @future methods cannot be called• Timing is determined by Salesforce• 5 concurrent jobs can process at once• 100 jobs can be in the queue

Page 27: Batch Apex in Salesforce

Demo

• Submitting• Scheduling• Monitoring

Page 28: Batch Apex in Salesforce

Questions?

davehelgerson.com @davehelgerson