batchapex-150603234116-lva1-app6892

Upload: chaitu215

Post on 28-Feb-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    1/28

    Batch Apex

    Dave Helgerson

    davehelgerson.com

    @davehelgerson

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    2/28

    Batch Apex Agenda

    High level description of Batch Apex

    How to use Batch Apex

    Simple Code Examples Demos in a Salesforce org

    Questions

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    3/28

    hat is Batch Apex!

    Batch Apex is a wa" to processmillions of records on the Salesforceplatform

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    4/28

    How does Batch Apex wor#!

    Start $" de%ning a dataset to processthrough Data$ase.Quer"&ocator

    Custom 'terator

    &ist Collection

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    5/28

    How does Batch Apex wor#!(cont.)

    *hen+ the dataset is divided intosmaller chun#s of records Each chun# of dataset processes as a

    separate transaction

    Si,e of each chun# is de%ned whensu$mitting the Batch process

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    6/28

    How does Batch Apex wor#!(cont.)

    -inall"+ after all chun#s of the datasethave $een processed+ postprocessing can $e run Send Emails

    pdate aggregate totals

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    7/28

    How does Batch Apex wor#!(cont.)

    De%ne the dataset

    Divide and process

    /ost processing

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    8/28

    How does Batch Apex run!

    As"nchronousl"+ of course0

    &i#e @future methods or Queuea$leApex

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    9/28

    hat conditions are ideal for Batch

    Apex!

    *as#s that involve large datasets orare processing intensive

    Source of data is from a singledata$ase o$1ect+ and Dataset can $eretrieved with a single S2Q&statement

    Each unit of wor# is independent

    3ot time4critical

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    10/28

    5ive me scenarios for Batch Apex

    processing

    6ecord 2wnership 6eassignment

    Data 6etention

    Data Cleansing 6ecalculating Apex 7anaged Sharing

    2ther 7ass 6ecord pdate

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    11/28

    hat does the code loo#li#e!

    Class de%nition implements theDatabase.Batchableinterface

    *hree methods are re8uired start()

    execute()

    finally()

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    12/28

    hat does the code loo# li#e!(cont.)

    Quer"&ocator Example

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    13/28

    hat does the code loo# li#e!(cont.)

    &ist Example

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    14/28

    hat does the code loo# li#e!(cont.)

    'tera$le Example

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    15/28

    hat does the code loo# li#e!(cont.)

    Additional Class Attri$utes Database.AllowsCallouts

    9:: callouts per transaction (inter ;9

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    16/28

    How do ' run a 1o$!

    Batch Apex is invo#ed programmaticall"with Database.executeBatch orSystem.scheduleBatch

    =isualforce page controller Apex trigger

    A1ax *ool#it

    Execute Anon"mous from -orce.com 'DE orDeveloper Console

    Scheduled >o$

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    17/28

    How do ' run a 1o$! (cont.)

    Database.executeBatch

    Su$mits a 1o$ to the 8ueue

    /arameters? 'nstance of the class

    Scope (optional)

    Example? Database.executeBatch(new xam!leBatch()"

    #$$)%

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    18/28

    How do ' run a 1o$! (cont.)

    System.scheduleBatch

    Creates a scheduled 1o$

    /arameters?

    'nstance of the class>o$ name

    *ime interval+ in minutes+ after which the 1o$should start executing

    Scope (optional) Example?

    System.scheduleBatch(new xam!leBatch()"

    &'est&" " #$$)%

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    19/28

    How do ' run a 1o$! (cont.)

    >o$ is su$mitted to the 8ueue < 1o$s can $e processing at once

    9:: 1o$s can $e in the 8ueue at once. -lex Queue (Spring 9o$ 'd is returned*rac# progress $" the 1o$ 'd

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    20/28

    How do ' schedule a 1o$!

    Scheduling from the ' Setup 4 Develop 4 Apex Classes 4 Schedule

    Apex $utton

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    21/28

    How do ' schedule a 1o$!(cont.)

    Scheduling in Apex system.schedule()

    3ame

    Cron expression 'nstance of Schedula$le class

    Example? system.schedule(&y Scheduled *ob&"

    &$ # + + + ,&" new xam!leSchedulable())%

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    22/28

    How do ' monitor a 1o$!

    Setup 4 7onitor 4 >o$s 4 Apex >o$s

    Shows active and completed 1o$information

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    23/28

    How do ' monitor a 1o$!(cont.)

    As"ncApex>o$ Accessi$le in Apex

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    24/28

    How do ' test Batch Apex!

    se *est.start*est() and*est.end*est()

    As"nchronous processes runs"nchronousl" when the

    *est.end*est() command executes

    *he execute() method ma" onl" $e

    invo#ed once

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    25/28

    How do ' test Batch Apex!

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    26/28

    Considerations Summar"

    Quer"&ocator 4 maximum of

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    27/28

    Demo

    Su$mitting

    Scheduling

    7onitoring

  • 7/25/2019 batchapex-150603234116-lva1-app6892

    28/28

    Questions!

    davehelgerson.com

    @davehelgerson