2

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 produces 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.

3
  • 4
    Is your test class using @IsTest(SeeAllData=true)? By default a test class can't see the data within an org. Ideally you would script in the required testing data. Commented May 13, 2014 at 23:01
  • No problem. This is a fairly common problem for people to encounter when starting out with tests. If you don't know it is occurring it can be hard to tell what going on. Note that this question may or may not be marked as a duplicate based on this being a very common problem. No offence I hope. Commented May 14, 2014 at 0:20
  • Possible duplicate of When testing can I not use existing Data? Commented May 20, 2014 at 0:21

1 Answer 1

6

As per my comment, to see data that is in the current Org you need to use @IsTest(SeeAllData=true). By default a test class can't see that data within an org.

Ideally you should aim to create portable tests cases. That being, test cases that aren't dependant on data already being in the org.

To do this you will typically script in any test data you need.

See also: Isolation of Test Data from Organization Data in Unit Tests

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .