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
John NeffJohn Neff 

Error: Variable does not exist but it DOES exist!

Hello, 

I am trying to write a test class for a VF controller, and I am getting an error saying the variable does not exist even though it does!  Why would this be happening? 

Here is my controller: 
 
public class DailySalesReportController{ 

        public List<Opportunity> listOfCefOpToday {get;set;}
        public List<Opportunity> listOfLoganOpToday {get;set;}
        public List<Lead> listOfCefLdToday {get;set;}
        public List<Lead> listOfLoganLdToday {get;set;}
        public List<Sales_Meetings__c> listOfCefSkedToday {get;set;}
        public List<Sales_Meetings__c> listOfLoganSkedToday {get;set;}
        public List<Sales_Meetings__c> listOfCefOcToday {get;set;}
        public List<Sales_Meetings__c> listOfLoganOcToday {get;set;}
        public Opportunity CefOpToday {get;set;}
        public Opportunity LoganOpToday {get;set;}
        public Lead CefLdToday {get;set;}
        public Lead LoganLdToday {get;set;}
        public Sales_Meetings__c CefSkedToday {get;set;}
        public Sales_Meetings__c LoganSkedToday {get;set;}
        public Sales_Meetings__c CefOcToday {get;set;}
        public Sales_Meetings__c LoganOcToday {get;set;}
    
    public DailySalesReportController(){
        listOfCefOpToday = [Select id, name from Opportunity WHERE Created_Today__c = TRUE AND Owner_Name__c = 'Jeff Cefalu'];
        listOfLoganOpToday = [Select id, name from Opportunity WHERE Created_Today__c = TRUE AND Owner_Name__c = 'Logan Connolly'];
        listOfCefLdToday = [Select id, name from Lead WHERE Created_Today__c = TRUE AND Owner_Name__c = 'Jeff Cefalu'];
        listOfLoganLdToday = [Select id, name from Lead WHERE Created_Today__c = TRUE AND Owner_Name__c = 'Logan Connolly'];
        listOfCefSkedToday = [Select id, name from Sales_Meetings__c WHERE Created_Today__c = TRUE AND Owner_Name__c = 'Jeff Cefalu'];
        listOfLoganSkedToday = [Select id, name from Sales_Meetings__c WHERE Created_Today__c = TRUE AND Owner_Name__c = 'Logan Connolly'];
        listOfCefOcToday = [Select id, name from Sales_Meetings__c WHERE Occurred_Today__c = TRUE AND Owner_Name__c = 'Jeff Cefalu'];
        listOfLoganOcToday = [Select id, name from Sales_Meetings__c WHERE Occurred_Today__c = TRUE AND Owner_Name__c = 'Logan Connolly'];
        
        
}
}

And here is my test: 
 
@isTest(seeAllData = true)
public class DailySalesReportControllerTest{
    // Unit test Method
    static testmethod void UnitTest() {
        //Create your Opportunity record with required field
        //Opportunity b = new Opportunity(Created_Today__c = TRUE);
        //insert b;
        test.startTest();
           DailySalesReportControllerTest ub = new DailySalesReportControllerTest();
           
           if(ub.listOfCefOpToday!=null && !ub.listOfCefOpToday.isEmpty())
           Opportunity cefOpportunity = ub.listOfCefOpToday.get(0);
           
           if(ub.CefOpToday !=null)
            String CefOpTodayId = ub.CefOpToday.Id;
           
        test.stopTest();
    }   
}

I keep getting this error: 
 
Error: Compile Error: Variable does not exist: listofcefoptoday at line 12 column 41

Can anyone help?  I'm really stuck here 
Best Answer chosen by John Neff
PawanKumarPawanKumar
I think you are creating test class instance instead of controller 'DailySalesReportController ' instance at line #9 in your screenshot.. Please use below code it will work.


@isTest(seeAllData = true)
public class DailySalesReportControllerTest {
 // Unit test Method
 static testmethod void UnitTest() {
  //Create your Opportunity record with required field
  //Opportunity b = new Opportunity(Created_Today__c = TRUE);
  //insert b;
  test.startTest();
  //DailySalesReportControllerTest ub = new DailySalesReportControllerTest();
  DailySalesReportController ub = new DailySalesReportController();

  if (ub.listOfCefOpToday != null && !ub.listOfCefOpToday.isEmpty())
   Opportunity cefOpportunity = ub.listOfCefOpToday[0];

  if (ub.CefOpToday != null)
   String CefOpTodayId = ub.CefOpToday.Id;

  test.stopTest();
 }
}

Regards,
Pawan Kumar

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try to update your code like below
@isTest(seeAllData = true)
public class DailySalesReportControllerTest{
    // Unit test Method
    static testmethod void UnitTest() {
        //Create your Opportunity record with required field
        //Opportunity b = new Opportunity(Created_Today__c = TRUE);
        //insert b;
        test.startTest();
           DailySalesReportControllerTest ub = new DailySalesReportControllerTest();
           
           if(ub.listOfCefOpToday!=null && !ub.listOfCefOpToday.isEmpty())
           Opportunity cefOpportunity = ub.listOfCefOpToday[0];
           
           if(ub.CefOpToday !=null)
            String CefOpTodayId = ub.CefOpToday.Id;
           
        test.stopTest();
    }   
}

Let us know if this will help you
John NeffJohn Neff
thanks Amit, I am still receiving the same error when I compile your code above 
John NeffJohn Neff
Yes, thank you so much for your help. 

Here is the error: 

User-added image
PawanKumarPawanKumar
I think you are creating test class instance instead of controller 'DailySalesReportController ' instance at line #9 in your screenshot.. Please use below code it will work.


@isTest(seeAllData = true)
public class DailySalesReportControllerTest {
 // Unit test Method
 static testmethod void UnitTest() {
  //Create your Opportunity record with required field
  //Opportunity b = new Opportunity(Created_Today__c = TRUE);
  //insert b;
  test.startTest();
  //DailySalesReportControllerTest ub = new DailySalesReportControllerTest();
  DailySalesReportController ub = new DailySalesReportController();

  if (ub.listOfCefOpToday != null && !ub.listOfCefOpToday.isEmpty())
   Opportunity cefOpportunity = ub.listOfCefOpToday[0];

  if (ub.CefOpToday != null)
   String CefOpTodayId = ub.CefOpToday.Id;

  test.stopTest();
 }
}

Regards,
Pawan Kumar
This was selected as the best answer
John NeffJohn Neff
Thank you Pawan!  How on earth did I not see that!? I drove myself crazy over that!