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
chanti kchanti k 

When we will getting list has no errors ?

Hi Experts,
Many times am getting this error on visualforce page and test calss ,When we will getting list has no errors ?what kind of situation ? how to we can resolved, kindly do the needful


Thank you,
Chanti
hitesh90hitesh90
Hello,

This exception is because of you are not getting any record in your SOQL query. see below link:
https://developer.salesforce.com/forums/?id=906F000000094QZIAY

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
VineetKumarVineetKumar
You get this error when the SOQL query that your code is firing is not returning any row, but you are still assigning it to an instance of an object.
Something like below :
Account accountObject = [SELECT Id FROM Account WHERE Name = 'Test Account'];

Best way to handle such cases is to put a check to such scenarios.
Something like below
List<Account> accountList = [SELECT Id FROM Account WHERE Name = 'Test Account'];
if(!accountList.isEmpty()){
    // put your business logic here.
}

Let me know if it helps.
NagendraNagendra (Salesforce Developers) 
Hi Chanti k,

Please find the explanation below with a suitable example why you are getting this error "list has no rows for assignment.

Whenever you try to write an SOQL Query, it means that you seek to fetch some data from a particular object.

While a SELECT normally returns an array/list, these statements are using the shorthand syntax that assumes only one row is returned.
What’s not obvious is that it also assumes that exactly one row is returned.
 
While this is unlikely to occur for Contact, it is highly likely to occur for any custom objects you create,
especially when a WHERE statement is used that might return zero rows, such as:
 
Player__c player = [SELECT Id from Player__c where Name = :username];
if (player != null)
 p = player.Id;
 
The above code will fail if there is no Player__c record with the matching username. It doesn't actually return a null.
 
It would be safer to do the following:
 
Player__c[] players = [SELECT Id from Player__c where Name = :username];
if (players.size() > 0)
p = players[0].Id;
 
It’s one of those situations for which you would not usually think of creating a test, so it’s safer just to avoid the possibility.

Best Practice:
It's always a best practice to check if the list is not empty before doing any DML operation.Your code could be modified so that it doesn't throw an exception:

Example :
Account accountObject = [SELECT Id FROM Account WHERE Name = 'Account Test Example'];
If there are no account records then it will throw an error that list has no rows for assignment to sobject.

Handling the error with best practice :
List<Account> mylist = [SELECT Id FROM Account WHERE Name = 'Account Test Example'];
if(!accountList.isEmpty()){
//write the logic which you want to perform if the accounts sobject is empty
}

For more information check the below link.
https://corycowgill.blogspot.com/2011/02/infamous-rookie-error.html

Please mark my solution as best answer if it helps you .................

Best Regards,
Nagendra.P