Apex Code Enhancements and Collections best practices
by Quinton Wall on January 29, 2010 at 03:45 PM
Spring '10 is just around the corner, with many great new features. One of the Apex Code enhancements which caught my attention was the limits on the number of items a collection can hold has been removed. The previous limit was set at 1000. Add this to the new ability to use generic sObjects to create a collection and Collections within the platform just got a whole lot more powerful.
As Ben Parker, the o-so-wise uncle of Peter Parker (aka Spiderman) once said: "with great power comes great responsibility." Changes to Collections is no different, and require some new thinking in regards to best practices.
The release notes, for example, mention that there is still a limit on heap size so care must be taken to not load up a collection and hit this limit. Good news is that a few other other Spring '10 changes can certainly be used to ensure you design your applications as efficiently as possible.
Here are just a few new SOQL clauses and functions which can be used with collections to help make the most fo the changes.
This is certainly not a complete list, but should certainly be something every developer is familiar with.TrackBack
TrackBack URL for this entry: http://www.typepad.com/services/trackback/6a00d8341cded353ef0120a82dd435970b
Listed below are links to weblogs that reference Apex Code Enhancements and Collections best practices:
Comments
Posted by Matt Kaufman on February 1, 2010 01:27 PM:
The previous limit on collections was 1000 not 200.
Posted by Quinton Wall on February 1, 2010 06:02 PM:
You are correct. I have updated the post.
Thanks for keeping me honest
Posted by David Cheng on February 8, 2010 02:45 PM:
Hi - can you tell me if aggregate queries are supported in Batch Apex?
Thanks
David
Posted by Evan Callahan on February 18, 2010 11:28 PM:
Looks like that may be a "no." I just tried one in a batch and got this error:
System.Exception: Aggregate query does not support queryMore(), use LIMIT to restrict the results to a single batch
Posted by Niki Vankerk on March 4, 2010 09:31 AM:
You can use aggregate queries in a batch.
My query looks like this:
AggregateResult[] groupedResults = [SELECT Featured_Site__c, count(id) cnt FROM SiteClick__c where Featured_Site__c in :sites GROUP BY Featured_Site__c];
I'm trying to count the number of records in the "SiteClick" table that are linked to each "Featured_Site", then I go on to update each Featured_Site with its count. The actual batch is querying the "Featured_Site" object since there are thousands of those, then my code will iterate through those in chunks of 200 to count up the SiteClicks for each.
Hope that helps.
Verify your Comment
Previewing your Comment
This is only a preview. Your comment has not yet been posted.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.

Posted by: |