+ Start a Discussion
LoganUTLoganUT 

Test Class Not Covering Code List Query

Hello everyone! 

I had this trigger and test class working well with 100% test coverage, then I added the Account__c field and now my test class is failing. The error I recieve is: System.QueryException: List has no rows for assignment to SObject. 

I have tried writing my query as a list on the Location_Master__c section, but it still did not work. I have tried just about everythign I can think of..

Below I have included a section of my trigger and test class:
 
trigger locStatusOpp on Opportunity (after update, after insert) { 
    
    List<Location_Detail__c> 	loctoInsert  	 = new List<Location_Detail__c>();
    List<Location_Wait_List__c> loctoWaitInsert  = new List<Location_Wait_List__c>();    
    List<Location_Master__c>  	locMaster 		 = new List<Location_Master__c>();
    
        for (Opportunity opp : Trigger.new) {
         if (opp.StageName == 'Closed Won' && opp.Location__c == 'Auburn Hills MI' && opp.Category__c != null && opp.Type_of_Charge__c == 'Subscription'){
            Location_Master__c locAuburnHillsMI = [SELECT Id FROM Location_Master__c WHERE Name = 'Auburn Hills MI' LIMIT 1];
            	   Location_Detail__c locNewAuburnHillsMI =  new Location_Detail__c(
           											     Name =  opp.Name,
           									   Account__c =  opp.AccountId,
           									  Location__c =  opp.Location__c,
          									   On_List__c =  1,
                       Location_Master__c =  locAuburnHillsMI.Id,
                       					Active__c =  True,
          								 Vendor_Type__c =  opp.Category__c);
                if(opp.Location__c == 'Auburn Hills MI'){
                loctoInsert.add(locNewAuburnHillsMI);
                    }
         }
 
@istest  
public class locStatusOppTEST {
    
static testMethod void locStatusOppTEST1(){
        List<Location_Detail__c> loctoInsert  = new List<Location_Detail__c>();
    
        Account acc = new Account();
            acc.Name = 'Test';
            insert acc;    
        
        Location_Master__c locMaster = new Location_Master__c();
            locMaster.Name = 'Auburn Hills MI';
            insert locMaster;
        
        Opportunity opp = new Opportunity();
            opp.Name              = 'Test';
            opp.AccountId         =  acc.Id;
            opp.Type_of_Charge__c = 'Subscription';
            opp.Category__c       = 'Cake Decorator';
            opp.Location__c       = 'Auburn Hills MI';
            opp.StageName         = 'Closed Won';
            opp.CloseDate         = date.newInstance(2017, 2, 20);
            insert opp;

    }

 
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below and remove SOQL from for Loop
trigger locStatusOpp on Opportunity (after update, after insert) 
{ 
    List<Location_Detail__c> 	loctoInsert  	 = new List<Location_Detail__c>();
    List<Location_Wait_List__c> loctoWaitInsert  = new List<Location_Wait_List__c>();    
    List<Location_Master__c>  	locMaster 		 = new List<Location_Master__c>();

	List<Location_Master__c> ListlocAuburnHillsMI = [SELECT Id FROM Location_Master__c WHERE Name = 'Auburn Hills MI' LIMIT 1];
    
	for (Opportunity opp : Trigger.new) 
	{
		if (opp.StageName == 'Closed Won' && opp.Location__c == 'Auburn Hills MI' && opp.Category__c != null && opp.Type_of_Charge__c == 'Subscription')
		{
			if(ListlocAuburnHillsMI.size() > 0 )
			{
				Location_Detail__c locNewAuburnHillsMI =  new Location_Detail__c();
				locNewAuburnHillsMI.Name =  opp.Name,
				locNewAuburnHillsMI.Account__c =  opp.AccountId,
				locNewAuburnHillsMI.Location__c =  opp.Location__c,
				locNewAuburnHillsMI.On_List__c =  1,
				locNewAuburnHillsMI.Location_Master__c =  ListlocAuburnHillsMI[0].Id,
				locNewAuburnHillsMI.Active__c =  True,
				locNewAuburnHillsMI.Vendor_Type__c =  opp.Category__c;
				
				if(opp.Location__c == 'Auburn Hills MI')
				{
					loctoInsert.add(locNewAuburnHillsMI);
				}
			}
		}
	}

Then try below test class
@istest  
public class locStatusOppTEST {
    
	static testMethod void locStatusOppTEST1()
	{
        List<Location_Detail__c> loctoInsert  = new List<Location_Detail__c>();
    
        Account acc = new Account();
            acc.Name = 'Test';
        insert acc;    
        
		
        Location_Master__c locMaster = new Location_Master__c();
            locMaster.Name = 'Auburn Hills MI';
        insert locMaster;
        
		
        Opportunity opp = new Opportunity();
            opp.Name              = 'Test';
            opp.AccountId         =  acc.Id;
            opp.Type_of_Charge__c = 'Subscription';
            opp.Category__c       = 'Cake Decorator';
            opp.Location__c       = 'Auburn Hills MI';
            opp.StageName         = 'Closed Won';
            opp.CloseDate         = date.newInstance(2018, 2, 20);
			
        insert opp;

    }
}


Let us know if this will help you
 
LoganUTLoganUT
Amit Chaudhary 8,

The test class fails right away on Location_Detail__c.

Any other ideas?

Thanks! 
Amit Chaudhary 8Amit Chaudhary 8
Can you please post the screen shot of your error ?
LoganUTLoganUT
Hope this helps....


User-added image
LoganUTLoganUT
I belive that this is the correct screen shot actually 
User-added image