+ Start a Discussion
Vinod AgrawalVinod Agrawal 

System.QueryException List has no rows for assignment to SObject

Hi, I am new to SalesForce Development. I wrote a trigger and a test class for it. Below is the code for the test class. When I execute the test class I get System.QueryException: List has no rows for assignment to SObject. First I thought I had some problem with my query but I executed it using Query Editor and the record returned successfully. I would appreciate if you can help me with this.

@isTest
private class NumberOfAttendeesUpdateTestClass{

    static testMethod void validateNumberOfAttendeesUpdate(){
   
        Contact ctc = new Contact(FirstName='Test',LastName='Contact', Conference__c='a00o0000003jzAwAAI');
        Conference__c cfc = [SELECT Id, Name, Number_of_Attendees__c from Conference__c WHERE Id = 'a00o0000003jzAwAAI' LIMIT 1];
        Double na = cfc.Number_of_Attendees__c;
        System.debug('Number of Attendees in Conference ' + cfc.Name + ' before inserting ' + cfc.Number_of_Attendees__c);
       
        insert ctc;
       
        cfc = [SELECT Id, Name, Number_of_Attendees__c from Conference__c WHERE Id = 'a00o0000003jzAwAAI' LIMIT 1];
        System.debug('Number of Attendees in Conference ' + cfc.Name + ' before inserting ' + cfc.Number_of_Attendees__c);
       
        System.assertEquals(na, na + 1);
   
    }
}
Best Answer chosen by Vinod Agrawal
Anoop yadavAnoop yadav
Hi,

Use @isTest(SeeAllData = true) instead of @isTest and then try.
One More thing You should create Test Data in your test class.

Check these Links
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

All Answers

Anoop yadavAnoop yadav
Hi,

Use @isTest(SeeAllData = true) instead of @isTest and then try.
One More thing You should create Test Data in your test class.

Check these Links
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
This was selected as the best answer
Vinod AgrawalVinod Agrawal
Thanks Anoop!! That resolved it and thanks for the tip!