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
karthikakarthika 

SOQL query does not return rows in Apex class

Hi All,

 

I have a strange issue. I have a SOQL query which is returning a single row of data if I run the query in the Salesforce Schema in Eclipse, but it is not returning any rows if I run the Query in the Apex class. I am not getting any rows and I get the exception:

 

System.QueryException: List has no rows for assignment to SObject.

 

Select Name, Id, BigMachines_Quote__r.Is_Primary__c, BigMachines_Quote__r.Id,q.BigMachines_Quote__c From Quote_Product__c q  where id='a2p50000000kDl4' 

 

Here BigMachines_Quote__c is the MAster and Quote_Product__c is teh child object. Any help is greatly appreciated.

 

Note: (I donot have the keyword with Sharing and the class is being executed in sytem mode, the reason for hardcoding the id is just  for testing purposes)

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
vriavmvriavm

ok Karthika tat is the problem.

You have two options. Either you create your own record in test class instead of querying from the DB which is a good practise to do.

Else the only option you have is create a new class for testing it.

For this all you do is create below class


@isTest(SeeAllData=true)
private class Test_QuoteRun {
    public static testMethod void runTestQuote() {
    //Instantiate the class and call the function where soql query is run
    }
}

 

Please mark this as solution if this solver your problem.

 

All Answers

vriavmvriavm

Hi,

      Can you check isdeleted flag for the record?

vriavmvriavm

Also just to amke it consistent use alias for all variables or dont use it at all for better coding practice.

I mean the q alias for the object in SOQL query.

karthikakarthika

Hi,

 

It is not a deleted record and hey thanks for pointing out the alias representation.

 

Still the same issue, I can see the record from SFDC Schema in eclipse for the SOQL Query and not from Apex class.

 

Thanks

vriavmvriavm

Is it possible for you to share the code to take a deeper dive into it?

 

karthikakarthika

Hi,

 

It is just an apex class and I have defined a function where I am performing the mentioned query and wanted to iterate over the list and do some aggregation, did not get a chance to finish my code as I am struck at this point. It is jus the function with couple of debug messages after the query.

 

Here is the query again:

 

Select Name, Id, BigMachines_Quote__r.Is_Primary__c, BigMachines_Quote__r.Id,q.BigMachines_Quote__c From Quote_Product__c  q where id='a2p50000000kDl4'.

 

Thanks

vriavmvriavm

If it is apex test class please add @isTest(SeeAllData=true)

karthikakarthika

I did not create a seperate class for test, instead I have created a function,

 

public static testMethod void runTestQuote() {

//Instantiate the class and call the function where soql query is run

 

}

 

I am new to Apex and I am not sure how to create a separate test class. Appreciate ur help.

 

 

Thanks

vriavmvriavm

ok Karthika tat is the problem.

You have two options. Either you create your own record in test class instead of querying from the DB which is a good practise to do.

Else the only option you have is create a new class for testing it.

For this all you do is create below class


@isTest(SeeAllData=true)
private class Test_QuoteRun {
    public static testMethod void runTestQuote() {
    //Instantiate the class and call the function where soql query is run
    }
}

 

Please mark this as solution if this solver your problem.

 

This was selected as the best answer
karthikakarthika

That Worked !!!!!!!!! I am able to see from debug log that the SOQL query has resulted in one row which is true.

 

Thanks a ton.

 

 

But can you explain what was I doing wrong here because pretty much I am doing the same thing except that I have not defined a seperate testClass for it instead calling a test method from the same class.

 

 

Thanks

 

 

vriavmvriavm

Cool the issue is by default when you create a test class you will not see all data in org,  you will be able to see only data you are creating in the test class context.
So the best practice is always you dont take sample data from org untinunless you sepcify (SeeAllData=true) which will override and provide visibility to all data in org.
If you need more information/help on this please chat with me @ vriavm@gmail.com

If answered Mark as solved it might help someone in need.

 

M UmerM Umer
I am having the same issue and the accepted solution is not working for me.

This is my test class.
 
@isTest(SeeAllData=true)
private class CenlarAuthDemoTest 
{
    static testmethod void TestContactCreation() 
    {
        CenlarAuthDemo.CenlarAuthDemoMethod('0033h00000Ps3hSAAR');    
    }
}

This is how I am using it:
 
Contact defContact = [select id, firstname, lastname from contact where id='0033h00000Ps3hSAAR'];

It is giving same error " System.QueryException: List has no rows for assignment to SObject". 

I am new to these forums and not sure if I can ask this question on an accepted answer but as option is there so i asked.