The Silver Lining

A developer's view of Cloud Computing platforms & technologies.

Handling System.QueryException

with 5 comments

When attempting to fetch a single record from the database it is easy to run into the above exception. I’ve seen two schools of thought in dealing with this issue – mostly in the forums – and have been hoping to find a more official standpoint.

I’ll describe the patterns and perhaps you can help me decide.

The Try-Catch Pattern

  1. Surround your query by a try-catch block.
  2. Fetch the record into a single object type variable.
  3. Either the query successfully executes, or the exception is caught and you deal with it appropriately.

An example,

MyObject__c obj;

try{

obj = [SELECT id FROM MyObject__c WHERE name=:previouslyDefinedVar];

} catch(System.QueryException e){

// Perform logic here

}

The ‘Only Use Lists For Query Results’ Pattern [Made the name up myself. True story.]

  1. Fetch records into Lists of objects. Do this even if you only expect a single row to be returned. [Queries that return values into Lists do not throw the above exception]
  2. Check the size of the list(perhaps with an if-statement) and only perform required actions on the record if the list has a size of 1.

List<MyObject__c> objects = [SELECT id FROM MyObject__c WHERE name=:previouslyDefinedVar];

if(objects.size()==1){

// Perform Logic

}

Personally I prefer the first approach. I’m not even really sure why, it just feels less hacky and more readable to me. Perhaps I’m wrong and you can point out why.

About these ads

Written by Wes

July 9, 2009 at 1:04 pm

5 Responses

Subscribe to comments with RSS.

  1. I agree with you regarding the first approach. It does feel less hacky and even I might say, more gooder.

    Jeff Douglas

    July 11, 2009 at 12:00 pm

  2. If you use the first method, then how are you going to know if an error occured? We cannot be in front of the comp checking debug logs all the time – esp when the code is in production. Would you have an outbound email sent out when the exceptions is caught?

    VJ

    August 3, 2009 at 6:08 am

    • That’s a good point. However I’m not really sure it applies to this situation. The second method wouldn’t yield any error either, and it could be argued that your code should be well tested before it goes into production, and therefore relatively error free.

      Of course no code is perfect so I would suggest doing more than coding system.debug() messages within the exception handler. Perhaps adding an informative error message using ‘ApexPages.Message’..

      Wes

      August 3, 2009 at 9:49 am

  3. I use both but have shifted to the first approach for all queries that expect only a single result back.

    d3developer

    September 21, 2009 at 3:11 pm

  4. Assigning the result of a SOQL query to a single object instance is part of the language and therefore is there to be used.

    In my opinion makes the code easier to read and maintain.

    I guess that this is one of the “structural vs literate programing” arguments.

    Nick Krastev

    May 13, 2011 at 4:17 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,850 other followers

%d bloggers like this: