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()

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 = ‘’;
update caseDetail[0];
} else {
// Query was empty

Written by 

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

Leave a Reply

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