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
vlrvlr 

SOQL query produces results in Execute Anonymous, but returns zero records when running a test

Hi,
Here is my method:

// return the list of all current students
    public List<Account> getAllCurrentStudents() {

        //get the applicant record type
        Id applicantRecordType = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Applicant').getRecordTypeId();
        Id enrolledStudentRecordType = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Enrolled Student').getRecordTypeId();

        System.debug('DEBUG, getAllCurrentStudents, applicantRecordType: ' + applicantRecordType);
        System.debug('DEBUG, getAllCurrentStudents, enrolledStudentRecordType : ' + enrolledStudentRecordType);

        List<Account> students = [Select c.FirstName, c.LastName,  c.RecordTypeId , c.USC_ID__c
                                  From Account c
                                  where c.RecordTypeId IN (:applicantRecordType, :enrolledStudentRecordType)];

        System.debug('DEBUG, getAllCurrentStudents, expected students: ' + students);
        System.debug('DEBUG, getAllCurrentStudents, expected students size : ' + students.size());

        return students;

    }

When I run in Execute Anonymous, it pro duces thousands of records:

Anonymous execution was successful.

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: 
Execute Anonymous: 
Execute Anonymous:         //get the applicant record type
Execute Anonymous:         Id applicantRecordType = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Applicant').getRecordTypeId();
Execute Anonymous:         Id enrolledStudentRecordType = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Enrolled Student').getRecordTypeId();
Execute Anonymous: 
Execute Anonymous:         System.debug('DEBUG, getAllCurrentStudents, applicantRecordType: ' + applicantRecordType);
Execute Anonymous:         System.debug('DEBUG, getAllCurrentStudents, enrolledStudentRecordType : ' + enrolledStudentRecordType);
Execute Anonymous: 
Execute Anonymous:         List<Account> students = [Select c.FirstName, c.LastName,  c.RecordTypeId , c.USC_ID__c
Execute Anonymous:                                   From Account c
Execute Anonymous:                                   where c.RecordTypeId IN (:applicantRecordType, :enrolledStudentRecordType)];
Execute Anonymous: 
Execute Anonymous:         System.debug('DEBUG, getAllCurrentStudents, expected students: ' + students);
Execute Anonymous:         System.debug('DEBUG, getAllCurrentStudents, expected students size : ' + students.size());
15:46:43.056 (56964179)|EXECUTION_STARTED
15:46:43.056 (56973541)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
15:46:43.064 (64060425)|USER_DEBUG|[7]|DEBUG|DEBUG, getAllCurrentStudents, applicantRecordType: 01230000001VKvTAAW
15:46:43.064 (64123109)|USER_DEBUG|[8]|DEBUG|DEBUG, getAllCurrentStudents, enrolledStudentRecordType : 012300000015e8sAAA
15:46:43.064 (64828823)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|select c.FirstName, c.LastName, c.RecordTypeId, c.USC_ID__c from Account c where c.RecordTypeId IN (:tmpVar1, :tmpVar2)
15:46:43.189 (189254531)|SOQL_EXECUTE_END|[10]|Rows:3667
15:46:43.197 (197056929)|USER_DEBUG|[14]|DEBUG|DEBUG, getAllCurrentStudents, expected students: (Account:{RecordTypeId=012300000015e8sAAA, FirstName=Test, Id=001c000000WAUEtAAP, LastName=Student}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=Colin, Id=001c000000YTf0YAAT, LastName=Student}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=Caitlin, Id=001c000000ZP7CsAAL, LastName=Pfaff, USC_ID__c=2147081986}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=Jane, Id=001c000000ZP7CtAAL, LastName=Lin, USC_ID__c=1942919830}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=Jasmin, Id=001c000000ZP7CuAAL, LastName=Amarillas, USC_ID__c=4974660115}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=Krystal, Id=001c000000ZP7CvAAL, LastName=Gamez, USC_ID__c=7850585793}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=Bridget, Id=001c000000ZP7CwAAL, LastName=Barni, USC_ID__c=3640986562}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=Jodi, Id=001c000000ZP7CxAAL, LastName=Lavoie, USC_ID__c=2538841530}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=Brittani, Id=001c000000ZP7CyAAL, LastName=Linder, USC_ID__c=2743497234}, Account:{RecordTypeId=012300000015e8sAAA, FirstName=April, Id=001c000000ZP7CzAAL, LastName=Kim, USC_ID__c=7449945743}, ...)
15:46:43.197 (197211578)|USER_DEBUG|[15]|DEBUG|DEBUG, getAllCurrentStudents, expected students size : 3667
15:46:43.959 (197277889)|CUMULATIVE_LIMIT_USAGE
15:46:43.959|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 3667 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 2 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

15:46:43.959|CUMULATIVE_LIMIT_USAGE_END

However, this code produces zero records during the test run:

15:50:00.692 (3692911643)|USER_DEBUG|[100]|DEBUG|DEBUG, getAllCurrentStudents, applicantRecordType: 01230000001VKvTAAW
15:50:00.692 (3692969908)|USER_DEBUG|[101]|DEBUG|DEBUG, getAllCurrentStudents, enrolledStudentRecordType : 012300000015e8sAAA
15:50:00.731 (3731254595)|USER_DEBUG|[107]|DEBUG|DEBUG, getAllCurrentStudents, expected students: ()
15:50:00.731 (3731387603)|USER_DEBUG|[108]|DEBUG|DEBUG, getAllCurrentStudents, expected students size : 0
15:50:00.731 (3731403872)|METHOD_EXIT|[109]|01pc00000006rPw|GenericCSVFileProcessor.getAllCurrentStudents()
15:50:00.731 (3731458148)|USER_DEBUG|[112]|DEBUG|DEBUG, insertNewUniqueDataToOrg, allStudents()

Please, help.

kevin lamkevin lam
You need to create your own test data in a unit test or use the isTest(SeeAllData=true) annotation.
Vamsi KrishnaVamsi Krishna
if your method is a test method or if its inside a test class, then it cannot access existing data unless you explicitly mark it with the attribute SeeAllData = true

please refer here for details
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm