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
}

(560)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in IBM Austraila as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 10 Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply