+ Start a Discussion
Timothy SmithTimothy Smith 

Error: List has no rows for assignment to SObject

When a Vacation_Package__c is created if Package_Type = 'Select,  set the Balance_Type to 'Nights' and set Sales_Company__c to 'Upgrade Sales'.  Upgrade Sales is an Account Record, this is a lookup field.

I am wondering why my SOQL search in the test class does not show any rows.  WHen I do a system.debug I can see that records are created, but my SOQL is not getting results.
 
trigger SelectVacationPackage on Vacation_Package__c (before insert, before update) {
List<Vacation_Package__c> PackagesToUpdate = new List<Vacation_Package__c>();
   
    List<Vacation_Package__c> WorkingPackages = [SELECT Id, Package_Type__c, Sales_Company__c, Package_Status__c
 FROM Vacation_Package__c WHERE Id In :Trigger.New];
   
   
    for(Vacation_Package__c vp: WorkingPackages){        
            if(vp.Package_Type__c == 'Select' && vp.Package_Status__c != 'Canceled'){
      PackagesToUpdate.add(vp);
        }
}
 
    for (Vacation_Package__c vsp : PackagesToUpdate){
     
        vsp.Sales_Company__c = [Select ID from Account WHERE Name = 'Upgrade Sales'].id;
        vsp.Balance_Type__c = 'Nights';
    }

        upsert PackagesToUpdate;      
  
}
@isTest
private class SelectVacationPackageTest {
private static testMethod void SelectVacations(){
   
        Account newAcc = FlowTestUtils.createHouseholdAccount();
        newAcc.Name = 'Upgrade Sales';
    	insert newAcc;
       
        Product2 newProd = new Product2();
        newProd.Name = 'Testing';
    	newProd.Family = 'Sub-Type';
        newProd.Select_Package_Sub_Type__c = True;
        insert newProd;
       
  List<Vacation_Package__c> packList = new List<Vacation_Package__c>();

       
        Vacation_Package__c vacPac = new Vacation_Package__c();
        vacPac.Service_Charge_Frequency__c = 'Annual';
        vacPac.Service_Charge_Start_Date__c = Date.today().addMonths(1).toStartofMonth();
        vacPac.Package_Type__c = 'Select';
        vacPac.Package_Status__c = 'Draft';
        vacPac.Household__c = newAcc.id;
        vacPac.Sale_Date__c = Date.today().addMonths(1).toStartofMonth();
        vacPac.Package_Sub_Type__c = newProd.Id;
       
        packList.add(vacPac);
           
        Vacation_Package__c vacPac1 = new Vacation_Package__c();
        vacPac1.Service_Charge_Frequency__c = 'Annual';
        vacPac1.Service_Charge_Start_Date__c = Date.today().addMonths(1).toStartofMonth();
        vacPac1.Package_Type__c = 'Full';
        vacPac1.Package_Status__c = 'Draft';
        vacPac1.Household__c = newAcc.id;
        vacPac1.Sale_Date__c = Date.today().addMonths(1).toStartofMonth();    
        vacPac1.Package_Sub_Type__c = newProd.Id;
        packList.add(vacPac1);

    System.debug('vacPac' + vacPac);
    System.debug('vacPac1' + vacPac1);
  //both debugs show new records as expected
    
        Test.startTest();
        upsert packList;
        Test.stopTest();
       
    
    Account testAcc = [SELECT Name FROM Account WHERE Id=:vacPac.Id Limit 1];
    System.debug('Testing Acc' + test);
//No results for testAcc   

       /*
       //I am receiving the error here:

		System.assertEquals('Upgrade Sales', [SELECT Name FROM Account WHERE Id=:vacPac.Id Limit 1].Name);  
		System.assertNotEquals('Upgrade Sales', [SELECT Name FROM Account WHERE Id=:vacPac1.Id Limit 1].Name);        
      	
*/
    }
}




 
OFröhlichOFröhlich
Hi,

in your Select you use vacPac.Id, which couldn't be an Account Id. Change it to vacPac.Household__c and it should give a result.
and:
System.debug('Testing Acc' + testACC);

If this helps, please mark as Best Answer to help others too.