0

In apex class (in constructor) this block of code:

        String xxx = 'data123'; // hardcoded
        xxx = xxx + '%';
        List<Interface__c> records = [SELECT Name FROM Interface__c where Name LIKE :xxx limit 50000];
        System.debug('record = ' +  records);

returns correct records. But in Apex class this returns only this:

records = ()

What may be the reason of this and how can I retrieve records in Apex class? Code for unit test method body looks like first code snippet.

1
  • 1
    You need to create your records. Test methods, unless marked with seeAllData=true (not recommended), they need you to create your test data. You start the test with pretty much empty sObjects Commented Jun 12, 2020 at 9:40

1 Answer 1

2

Starting with Apex code saved using Salesforce API version 24.0 and later, test methods don’t have access by default to pre-existing data in the organization, such as standard objects, custom objects, and custom settings data, and can only access data that they create. However, objects that are used to manage your organization or metadata objects can still be accessed in your tests such as:

  • User
  • Profile
  • Organization
  • AsyncApexJob
  • CronTrigger
  • RecordType
  • ApexClass
  • ApexTrigger
  • ApexComponent
  • ApexPage

Whenever possible, you should create test data for each test. You can disable this restriction by annotating your test class or test method with the IsTest(SeeAllData=true) annotation.

Read more here:- 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 .