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
JJE_OLDJJE_OLD 

SOQL return no rows

Hi,

 

I'm trying to write an apex test class

 

In eclipse Schema page I type the following SOQL request:

 

SELECT c.id FROM case c WHERE c.subject = 'Test Case' LIMIT 1

It works but when I'm trying to test this code:

@isTest
private class testCaseTrigger {
    static testMethod void testCase() {
        Test.startTest();
        Case cs = [SELECT c.id FROM case c WHERE c.subject = 'Test Case' LIMIT 1];
        
        cs.CR_Delivery_Version__c = '7.2';
        update cs;
        
        Case newCase = [SELECT crtargetrelease__c FROM case WHERE id =: cs.id];
        system.assertEquals('MEGA 2009', newCase.CRTargetRelease__c);    
        
        Test.stoptest();
    }
}

 I get the following error : 'List has no rows for assignement to SObject'

 

I also tried the following: 'Select id FROM case', and I get also an empty list.

What is the problem?

 

thanks,

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JJE_OLDJJE_OLD

The Support told me to lower my Class API Version.

After changing from 24 to 20, it worked.

All Answers

sai.sfsai.sf

Its looking for case record with subject as 'Test Case'.Please make sure u have that record in your sandbox.Then run the test class.

JJE_OLDJJE_OLD

Yes, I tried the Request in eclipse and it returned 1 row.

I also tried to replace it with 'Select Id FROM Case' and it returned nothing, but I have 20 cases in my sandbox.

 

What could be the problem here?

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

Can u post ur Trigger.

 

Regards,

Rajesh.

JJE_OLDJJE_OLD

The problem is not with the trigger wich is working fine in the sandbox but with the request:

Case cs = [SELECT c.id FROM case c WHERE c.subject = 'Test Case' LIMIT 1];

Which is retruning nothing.

Rajesh SriramuluRajesh Sriramulu

Hi

 

Try to insert the case manullay whose

c.subject = 'Test Case'

 

and u will get and if u r writing the test class for trigger means plz post ur trigger code.

 

Regards,

Rajesh.

ShailforceShailforce
@isTest(seeAllData=true)
private class testCaseTrigger {
    static testMethod void testCase() {
        Test.startTest();
        Case cs = [SELECT c.id FROM case c WHERE c.subject = 'Test Case' LIMIT 1];
        
        cs.CR_Delivery_Version__c = '7.2';
        update cs;
        
        Case newCase = [SELECT crtargetrelease__c FROM case WHERE id =: cs.id];
        system.assertEquals('MEGA 2009', newCase.CRTargetRelease__c);    
        
        Test.stoptest();
    }
}
JJE_OLDJJE_OLD

The Support told me to lower my Class API Version.

After changing from 24 to 20, it worked.

This was selected as the best answer
Heath LHeath L

Does anyone know why this is true? This board provides solutions often times without explanation. I would like to know why API version 24 and 25 does not let you query in test classes. I have run into this so many times!

JJE_OLDJJE_OLD

I don't know why, but I suppose that the goal was to isolate the test from the real data, maybe to separate the deployment as they allow since winter 13 to use test data in Static resources.

But as was suggested in this post, you could alway s use the following option to access the production data.

 

regards,

 

@isTest(seeAllData=true)