function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kim D.ax1409Kim D.ax1409 

"Content cannot be displayed: List has no rows for assignment to Sobject" error message

Our portal users see this error in the comments section of their case and I need help understanding what could cause this problem.  This issue happens with our custom comment, the standard SF comment system works fine.

 

This issue was reported to us from our customers starting today, 5/15/12.  Did something changed over the weekend that could cause this?  I can send your our Apex script if needed.

 

Thanks in advance!

ForceCoderForceCoder

I don't know what would have changed but that error message means that there is a SOQL query that is not returning *exactly* one result in an assignment to a variable.

 

It is likely that the code assumes that there will always be a certain record present, but for some reason there isn't. 

 

SOQL queries return a List of records, but there is a shorthand syntax to assign the result of the query to a single varaible.  The catch is that the query must return *exactly* one record.  In your situation zero records are being returned.

 

Here's some code that illustrates the issue and shows an alternative:

// Will cause your error if there is no Account with an Id of '123456789012345678', because there will not be exactly one record.
Account acct = [
    Select Id, Name 
    From Account
    Where Id = '123456789012345678'
];

// Sometimes you'll see something like the below.  That will also cause the error if there are no matching accounts.
Account acct = [
    Select Id, Name
    From Account
    Where Name = 'Example'
    Limit 1
];

// A safer way to code for this situation is to assign the query to a list and then check to see if it is not empty.  For example:
List<Account> soqlAcct = [
    Select Id, Name
    From Account
    Where Name = 'Example'
    Limit 1
];

Account acct = null;
if (!soqlAcct.isEmpty()) {
    acct = soqlAcct.get(0);
} else {
    // perhaps do something for situation where no account found.
}

 

If you can reliably reporduce this you could start a debug log for the user reproducing it and get more specific information about the failure.  The code should probably be changed to handle the situation where there is no record if possible (since that is actually occuring); however, it may make no business sense to never have a record, so you might also have to add whatever record is missing.