You need to sign in to do that
Don't have an account?
ministe2003
SOQL no results - exception or no exception?!
Hello,
I'm struggling to come up with an answer to this by myself so thought Id throw it out there.
Sometimes if a SOQL query returns no results it throws an exception, which can be caught and then the field populated differently, ie created. However sometimes it doesn't throw an exception so I have to wrap my query in a try{catch} and then outside of that, also check to make sure the return isn't null so I can handle that too.
Is there a logical reason why it does this? It's making my code unnecessarily long and at best, is just annoying!
Thanks for any replies
Steven
In the first instance you are assigning the results into a variable that holds a single record, which according to the docs only works if the query returns exactly one element. Hence the exception.
In the latter, you are assigning the results to a list and its perfectly valid for a list to contain zero elements.
I'd say you have two choices:
1. Alter the first piece of code so that you store the results in a list, then you will be checking in both cases if there are any returned values.
2. Alter the second piece of code to try to access the element at [0] of the list - this will throw an exception if the list is empty.
Choice 1 is the correct one IMHO, as otherwise you're using exception handling for business logic.
All Answers
I wouldn't expect a SOQL query that returns no results to throw an exception - it sounds more likely to be how you are using the results - e.g. appending [0] to the query to retrieve the first result will throw an exception if there are no results.
Do you have an example piece of code that throws an exception?
This section works as the assignment from the query returns this exception:
However this block, right below the block you've just seen, doesnt return any value but doesnt throw the exception:
Below this I have to check the return manually.
In the first instance you are assigning the results into a variable that holds a single record, which according to the docs only works if the query returns exactly one element. Hence the exception.
In the latter, you are assigning the results to a list and its perfectly valid for a list to contain zero elements.
I'd say you have two choices:
1. Alter the first piece of code so that you store the results in a list, then you will be checking in both cases if there are any returned values.
2. Alter the second piece of code to try to access the element at [0] of the list - this will throw an exception if the list is empty.
Choice 1 is the correct one IMHO, as otherwise you're using exception handling for business logic.
OK thanks, so its a simple case of the variables I assign it to then. I understand now, before I was just guessing!
Thanks!
For a bit more clarity on your question asked
Suppose you have a query like below
In this case query1 won't throw an exception if the return is null, while query 2 will throw an exception.
Also if you accessed accounts1[0] ,it will also throw an exception.
Thanks and Regards,
Shiva RV