List has no rows for assignement to SObject Error in Salesforce Apex Class

Error: List has no rows for assignement to SObject in Salesforce,

My Case Object SOQL was something like this:
Case caseDetail = [select id, Subject from Case where id = :caseId]

If the SOQL query for some reason would not a return a result row the error message would be raised. A way to solve this is to assign the result of the SOQL-query to an caseDetail array instead.
Case[] caseDetail = [select id, Subject from Case where id = :caseId]

and then check with conditions using size() or isEmpty

caseDetail.size() OR caseDetail.isEmpty()

Example:
if (caseDetail.size() > 0) {
/* At least one row has been returned from the SOQL query
If we for example are only expecting one opportunity as result then get a handle on it like this */
caseDetail[0].subject = ‘TheBlogReaders.com’;
update caseDetail[0];
} else {
// Query was empty
}

Written by 

Salesforce MVP | Lightning Champion | 16x Salesforce Certified | Application Architect | 6x Trailhead Ranger | Techforce Services | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*