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.

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.

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.