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
IanM1963IanM1963 

Unit Test Throwing Weird Exception

Hi,

I'm having fun with this problem at the moment. I'm running Unit Tests and this doesn't seem to make sense because the initial error: System.Query.Exception: List has no rows for assignment to SObject

I thought I had remedied by enclosing the code in a try{}Catch{} block. However, I re-run the test and it still comes up with the same problem!

Here's my code:


Method in code…

 

public void dropInPool()
    {
    
           
        Id shiftId = ApexPages.CurrentPage().getParameters().get('shiftId');
        Id siteId = ApexPages.CurrentPage().GetParameters().get('siteId');
	
		
		
		try
		{
			targetShift = [select Id, Site_Plan__c,Colleague_Availability__c, Site_Plan__r.Account__c, Site_Plan__r.Account__r.Name, Colleague_Availability__r.Colleague__c, Colleague_Availability__r.Colleague__r.Name, Colleague_Availability__r.Colleague__r.Worker_First_Name__c, Colleague_Availability__r.Colleague__r.UK_Mobile_Number__c, Colleague_Availability__r.Availability__c, Shift_Start_Time__c, Account__c from Colleague_Shift__c where Site_Plan__r.Account__c  = :siteId and Id = :shiftId];
		
		
		
		        if (targetShift.Colleague_Availability__c != null)
		        {
		            try
		            {
		             
		                 oldAvailability = [select OldValue from Colleague_Availability__History 
		                                         where Field = 'Availability__c' 
		                                         and ParentId = :targetShift.Colleague_Availability__c order by CreatedDate desc limit 1];
		                                         
		                 tempAvailability = [select Availability__c, Confirmed__c from Colleague_Availability__c where Id = :targetShift.Colleague_Availability__c and Date__c = :convDate];
		                 tempAvailability.Availability__c = (String) oldAvailability.OldValue;
		                 tempAvailability.Confirmed__c = false;
		              }
		              catch (Exception e)
		              {
		                  oldAvailability = null;
		                  tempAvailability = [select Availability__c, Confirmed__c from Colleague_Availability__c where Id = :targetShift.Colleague_Availability__c and Date__c = :convDate];
		                  tempAvailability.Availability__c = 'NONE';
		                  tempAvailability.Confirmed__c = false;  
		              }
		            update tempAvailability;
		            targetShift.Colleague_Availability__c = null;
		            update targetShift;
		        }
		        	dailyShifts = null;
		        }
		catch (Exception e)
		{
			ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Cannot find Id! The Shift Id or Site Id has been lost somehow. Cannot move this Colleague, please try again.'));
		}
    }

And here's the testmethod...

 

private static testmethod void dropInPoolTest()
    {
	Test.setCurrentPage(new PageReference('/apex/shiftPlanningWithColleagueCreate'));
      
     	ShiftControllerWithColleagueCreate myController = new ShiftControllerWithColleagueCreate();
 
       Date testDate = Date.today();
       String sDate = String.valueOf(testDate);
       
       // Create a Site.
       
       Account myTestAccount = new Account();
       myTestAccount.Name = 'The test Account';
       insert myTestAccount;

       // Create a Site Plan.
       
       Site_Plan__c mySitePlan = new Site_Plan__c();
       mySitePlan.Account__c = myTestAccount.Id;
       mySitePlan.PlanDate__c = testDate;
       insert mySitePlan;
       
       // Create a Colleague.
       
       Colleague__c testColleague = new Colleague__c(Name = 'Test Colleague');
        insert testColleague;
  
        Junction__c myTestJunction = new Junction__c(Site__c=myTestAccount.Id, Colleague__c=testColleague.Id, Home__c='Home');
        insert myTestJunction;
       

            // Now create a week's availability for the Colleague
            List<Colleague_Availability__c> myAvList = new List<Colleague_Availability__c>();
            
            
            for (Integer i = 0; i <=7; i++)
            {
                myAvList.add(new Colleague_Availability__c(Name='testAvailability' + i, Availability__c = '09:00', Colleague__c = testColleague.Id, Date__c = testDate));
            }
            
            insert myAvList;
            Colleague_Shift__c testShiftWeAreBookedOn = new Colleague_Shift__c(Shift_Start_Time__c = '08:00', StartTime__c = testDate, Site_Plan__c = mySitePlan.Id, Colleague_Availability__c = myAvList[0].Id);
            insert testShiftWeAreBookedOn;

            // Now set the parameters for the method...

            ApexPages.CurrentPage().getParameters().put('shiftId', testShiftWeAreBookedOn.Id);
            ApexPages.CurrentPage().getParameters().put('siteId', null);
      
            // Now test the method...
            
            myController.dropInPool();
            
            System.AssertEquals(null,myController.targetShift.Colleague_Availability__c);
            System.AssertEquals(false, myController.tempAvailability.Confirmed__c);
 
    }

  I would massively appreciate it if anyone can see something that's causing this - I just can't see what the issue is. I thought the try - catch would have sorted it!


Rahul SharmaRahul Sharma

I think problem,Error not catched in try-catch may be due to use of try-catch inside try catch, why dont you use single try-catch block.

Or else you query in a list instead of directly querying in object, to avoid that error.

just a snippet:

 

 

List<Colleague_Shift__c > lstTargetShift = new List<Colleague_Shift__c >();
{
    if(!lstTargetShift.isEmpty())
    {
        // Perform your action's
        // use lstTargetShift[0]

 

pankaj.raijadepankaj.raijade

Hi 

 

Problem is not because of try catch blocks.

it is because while quering you are not using list object, in staind you are directly asinging it to the sobject. 

This error is thrown when the query returns zero row result.

use list of object to capture the query result and then use the object by list[0]

hemantgarghemantgarg

I agree with Pankaj, I faced same same type of issue many times.