You need to sign in to do that
Don't have an account?
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?
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
Hi,
I think you might be using any condition while fetching the records in the test class.
--
Sridhar Bonagiri
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.
try like this
list<Pais__c> lstp = [Select Id From Pais__c Where ClavePais__c = 'MEX'];
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
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 is great jesuscs.. Your solution fixed my problem.. Thanks a lot..
Salesforce has to fix this bug..
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.
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; } }
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...
Thanks, the @isTest(SeeAllData = true) saved me.
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
Thanks jesuscs!
It do really help me out!
Here it is July 2013, and the version # still has an issue to the very same problem.
Thank you for posting the solution.
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)
You wrote:
@isTest (SeeAllData = true) // make this as your first line of code this will help