I am currently writing test coverage for a batch file I have in apex. I'm using a @Testsetup method to avoid rewriting some info over and over. Withing my controller I have this the following line

List<RecordType> rTypes = [select Id from RecordType where sObjectType = 'Opportunity' and DeveloperName = :cParent.OppRecordType__c];

The error I am getting says DML actions cannot be done to RecordType. How do I insert dummy data or make the test execute that query?

static void prepareData(){
            RecordType rt = new RecordType();
            //assign values
            insert rt;

The above is my code within my test class.

  • 1
    Record type should be visible in tests. Can you provide peace of code, where you are getting this error Jun 8, 2017 at 17:36

2 Answers 2


It is good that you want to create your own test data, but in this case unnecessary. From the Apex Developer Guide:

Isolation of Test Data from Organization Data in Unit Tests

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.


you don't need to create RecordType in test. It is already accessible

You must log in to answer this question.

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