+ Start a Discussion
SandeepVSandeepV 

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

I am getting error while running Tests 

 

  /**
      * Method that creates the test estimate object
      */
     public static Services_Estimate__c createTestEstimate(){
         Product2 prod = [SELECT ID, NAME FROM Product2 WHERE NAME = 'BASE24-eps'];
        
        Services_Demand_Management__c dm= new Services_Demand_Management__c (Name = 'MyTestEstimate',
                                  Request_Status__c = DemandManagementHelper.STATUS_NEW,
                                  Request_State__c = DemandManagementHelper.STATE_OPEN,
                                  Comments__c = 'My Desc',
                                  ACI_Product1__c = prod.Id,
                                  Request_Type__c = 'Services',
                                  Oracle_Project_Number__c = 'TP00001',
                                  Oracle_Project_Name__c = 'Test Project',                                  
                                  Request_Sub_Type__c = ServicesEstimateHelper.IAE_FULL,
                                  Request_Submitter__c = UserInfo.getUserId());
         insert dm;

         Services_Estimate__c est= new Services_Estimate__c(Name = 'MyTestEstimate',
                                        Estimate_Status__c = DemandManagementHelper.STATUS_NEW,
                                        Request_Description__c = 'My Desc',
                                        Demand_Management_Request__c = dm.Id);
                                        
         
         insert est;
         
         Services_Review_Meeting__c meeting = new Services_Review_Meeting__c(Estimate_In_Review__c = est.Id,
                                        Meeting_Date__c = Date.today(),
                                        Meeting_Description__c = 'Review IA',
                                        Meeting_Goal__c = 'Reach Agreement',
                                        AD_Comments__c = 'Done',
                                        AD_Review_Disposition__c = 'Agree',
                                        Product_Review_Disposition__c = 'Agree',
                                        Sales_Review_Disposition__c = 'Agree',
                                        Services_Review_Disposition__c = 'Agree');
         insert meeting;
         return est;
        
     }
     
     public static testMethod void testReadEstimate(){
         
         Services_Estimate__c est= createTestEstimate();                                   
         
         readEstimates(est.Demand_Management_Request__c);
         readEstimate(est.Id);
         readEstimateItems(est.Id);         
         readReviewMeetings(est.Id);
         readEstimateFunctionalFeatureMappings(est.Id);
      
     }


}

 Message shows that error occurs at Line 252 which is line below , but this query is returning a single result ,I am not sure why this error is appearing. Can some one pls. help me ? 

Product2 prod = [SELECT ID, NAME FROM Product2 WHERE NAME = 'BASE24-eps'];
Best Answer chosen by Admin (Salesforce Developers) 
hemantgarghemantgarg

I think the test data is separated from org data now, so the org data is not accessible from test classes, to make it accessible it you have to use (@showAllData=true ) in the test class. But the best practice is to insert the test data in test class itself.

All Answers

hemantgarghemantgarg

I think the test data is separated from org data now, so the org data is not accessible from test classes, to make it accessible it you have to use (@showAllData=true ) in the test class. But the best practice is to insert the test data in test class itself.

This was selected as the best answer
ivetkinivetkin

Hi,

try 

List<Product2> prod = [SELECT ID, NAME FROM Product2 WHERE NAME = 'BASE24-eps'];

if (!prod.isEmpty()){}....


or Product2 prod = [SELECT ID, NAME FROM Product2 WHERE NAME = 'BASE24-eps' limit 1];


SandeepVSandeepV

Thanks Hemant ! 

 

That is what I was missing .,

 

Sandeep.