»
S
I
D
E
B
A
R
«
Calculating Totals with Batch Apex
August 13th, 2009 by Evan Callahan

Salesforce.com has a new feature called Batch Apex. It is currently available only as a preview by special request, but is expected to be in general release later this year. One of my nonprofit clients has a very large data set and have been looking forward to the ability to add up opportunity values in a variety of ways. We used Batch Apex to create a tool for them, and it seems to be working extremely well.

In this post, I will briefly explain what Batch Apex is, and describe our use case for the feature. In a later post, I will show the tool we created and how it works.

About Batch Apex
Apex has limits. You can perform only so many queries, and process only so many results; this allows Salesforce to keep your resource usage under control. Batch Apex blows these limits out of the water by allowing you to perform a query that returns up to 5 million records, and then create a special type of apex class that processes the records 200 at a time. There are still some rules as to what you can do in a Batch Apex class, but it can be a great workaround for apex limits.

Once you write a “Batchable” Apex class that follows the proper design pattern, you can use the class to execute your query, which queues up an Apex job that executes little by little until it is complete. How fast it runs depends on factors such as complexity of your query, time of day, and the extra capacity of the Salesforce system. You can monitor your job’s progress by clicking Setup | Monitoring | Apex Jobs.

Monitoring batch apex jobs

As you see, the last apex job shown was broken into 1,327 batches of 200 records – which means the query must have returned and processed over 265,000 opportunities.

Why We Used It
We use many strategies for summarizing data in Salesforce, including report totals, roll-up summary fields, and apex code in triggers. Because roll-up summaries do not support calculations of opportunity totals into contacts who have roles, we have always used triggers to keep these calculations current. However, if your data set is large enough, you will hit apex limits.

The following screen shows the donation summaries our client asked us to provide for all their donors, households, and accounts. Having this data available on the contact and household records will allow them to produce reports that filter in creative ways – for example, donors whose donations are rising or falling from year to year or those with one type of donation history but not another.

Opportunity totals rolled up into the contact record

With the tool we created, the client can visit a Visualforce Page (daily or weekly, perhaps) and kick off the Batch Apex that performs all the rollups. The Apex runs in steps: the first two batches roll up opportunities to a temp table, a third copies totals from the temp table to the contact, household, and account objects, and so on. When the jobs finish running, they know the totals are all correct. We still use triggers to add a newly closed opportunity to the totals – but because all the really heavy lifting now belongs to the offline Batch Apex process, we don’t expect to hit any limits.

In my next post, I will show you the Visualforce tool and a bit of the Batch Apex code I wrote.


5 Responses  
Mike Smith writes:
August 13th, 2009 at 8:27 am

Nice work. Looking forward to part 2.

Alex Hoffman writes:
August 13th, 2009 at 5:53 pm

Batch Apex is something that excites me.

One question – why calculate these totals as a batch process, rather than calculate them as part of a transaction when they occur?

    Anand Sethupathy writes:
    August 19th, 2009 at 10:39 pm

    The great promise in Batch Apex is as a means to work beyond the Apex Governor Limits.

Evan Callahan writes:
August 20th, 2009 at 12:44 pm

Great question, Alex. Anand has hit on the answer – while we can (and do) adjust some of these totals on the fly with triggers, redoing the entire set of totals is not possible within the Apex governor limits. The most common cases are pretty easy (creating and closing new opps), but what about when someone changes the amount or stage of an opp from a prior year? You have to recalculate their entire history to get the consecutive years of giving and so forth.

nonprofitCRM.org » Blog Archive » Batch Apex – Demo writes:
August 20th, 2009 at 1:12 pm

[...] is a 5-minute demo of the nifty Visualforce interface I created for running batch apex (see my earlier post on Batch Apex and why we are using it). I needed to create the custom page [...]

Leave a Reply

»  Substance: WordPress   »  Style: Ahren Ahimsa