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
jesuscsjesuscs 

List has no rows for assignment to sobject but the object has records

Hi, everyone

 

I need your help to understand and fix this problem:

I built an apex text class in a sandbox environment (i'm using eclipse) but when I run this apex test class I'm getting this error: System.QueryException: List has no rows for assignment to SObject. The problem is that the object really has records both in sandbox and in production environment !!!! I run that query in Force.com Explorer, salesforce.shema Explorer, etc. and there I can see the existing records but in the test all the time throws that error... Could anyone please guide me why is it happening and how can it be fixed?

Best Answer chosen by Admin (Salesforce Developers) 
jesuscsjesuscs

Hi

 

Thanks kiran_mutturu and Natraj, both are right and I implement both solutions however the test class continued throwing the same error but I can fix it changing the class api version. When I create the apex class it was created with the lastest api version (24) and when I change it to a lower version it works perfectly. I don´t know if it's a force.com ide bug or something else but the error isn't caused by the code.

 

Hope someone else can help this solution and thank you again.

All Answers

Sridhar BonagiriSridhar Bonagiri

Hi,

I think you might be using any condition while fetching the records in the test class.

 

--

Sridhar Bonagiri

jesuscsjesuscs

Hi 

The query is very simple, in this line is where i get the error:

 

Pais__c p = [Select Id From Pais__c Where ClavePais__c = 'MEX'];

 

but if i execute it in the force.com explorer, apex explorer, etc i get the value.

kiranmutturukiranmutturu

try like this

 

list<Pais__c> lstp = [Select Id From Pais__c Where ClavePais__c = 'MEX'];

NatrajNatraj

try this

 

@istest
 
 public class TestClass{

 public static testMethod void TestMethod(){

 

Pais__c  pa = new Pais__c(Name = 'Test',ClavePais__c = 'MEX');

insert pa;

 

//Add some system assert methods

 

Pais__c p = [Select Id From Pais__c Where ClavePais__c = 'MEX'];

}

 

}

 

This is how your test class should be

jesuscsjesuscs

Hi

 

Thanks kiran_mutturu and Natraj, both are right and I implement both solutions however the test class continued throwing the same error but I can fix it changing the class api version. When I create the apex class it was created with the lastest api version (24) and when I change it to a lower version it works perfectly. I don´t know if it's a force.com ide bug or something else but the error isn't caused by the code.

 

Hope someone else can help this solution and thank you again.

This was selected as the best answer
priyanka.mv26priyanka.mv26

This is great jesuscs.. Your solution fixed my problem.. Thanks a lot..

 

Salesforce has to fix this bug.. 

RaveenRaveen

Hi All,

This is because you test method cannot read all the existing data.

 

making your test method to read all data will fix this.

 

@isTest (SeeAllData = true)       // make this as your first line of code this will help 

 

 

happy Coding

Raveen.

Aneesha23Aneesha23

i just got the same error can any one help please....

 

 

 

@RestResource(urlMapping='/ListSubscriptionsForUsers/*') global with sharing class ListSubscriptionsForUsers{ //post annotation..... @HttpGet //method to return list of subscriptions for particular selected subscriber....... global static List<Document_Subscription__c> listSubscriptions(){ RestRequest req = RestContext.request; String mailId= req.params.get('email'); system.debug('mailId----->>>>'+mailId); Subscribers__c sub=[SELECT id, Name FROM Subscribers__c WHERE Email_Address__c=:mailId]; system.debug('sub----->>>>>>>>>>>>'+sub.Name); List<Document_Subscription__c> subscriptions= [SELECT Id,Name,Document_Id__c,Document_Name__c,Content_Type__c,Last_Sent__c, Document_Source_URL__c from Document_Subscription__c WHERE Subscriber__c =:sub.id]; return subscriptions; } }

Josh VJosh V

Thank you Raveen.  This is the proper method to solve your problem.  (SeeAllData = True) is a new method which was implemented as of API 24.0...

MicroshakMicroshak

Thanks, the @isTest(SeeAllData = true) saved me.

tomoktomok

Thanks jesuscs. I have been spending so much time on the reason why I get the same error and now it's been solved.

Thanks you again, 

Tomo

morgan.mamorgan.ma

Thanks jesuscs!

It do really help me out!

 

TheresaAndersonTheresaAnderson

Here it is July 2013, and the version # still has an issue to the very same problem. 

 

Thank you for posting the solution. 

riffindusriffindus
It gives no row exception because the soql is run based on user permission. if the user doesn't have permission to the object or field, user will get list retirned as 0. 
Hector SosaHector Sosa
@isTest(SeeAllData = true) sloved my problem :D
Preet Gujral 3Preet Gujral 3
Hi,

I am having a similar error and have tried the suggestions in this post with no success. I posted my question here to avoid cluttering this post in case the answer is different. Any help is appreciated!

Testing Controller Extension getting error "List has no rows for assignment to SObject" (https://success.salesforce.com/answers?id=9063A000000DkCdQAK)
archer0072archer0072
Raveen.... Thank you for making my day brighter!  100% coverage with your solution!

You wrote:

@isTest (SeeAllData = true)       // make this as your first line of code this will help