1

I am writting a test method for a certain code.This code is fetching Record Type detail from an Object Service_Form__c.

Class:

String recordTypeName;
Schema.DescribeSObjectResult srfDescribe = Service_Form__c.SObjectType.getDescribe();
   for (Schema.RecordTypeInfo recordType : srfDescribe.getRecordTypeInfos()) {
      if (recordType.isDefaultRecordTypeMapping()) {
         recordTypeName = recordType.Name;
      }
   }

With in the test class with seeAllData= true, without any effort, I am able to fetch all recordType deatil from the Object. However, if I am considering seeAlldata = false, then srfDescribe is coming null.

Is there any other way, so that we can access a Object's recordtype in the testMethods without inserting an Object record and assigning recordtypes to that record ?

  • I copied your code and placed it into an empty test, I was able to do a system.debug on the record type name and it prints out my default record type. Try just testing the above and add a debug to see what you get, or add your test class to your question to see if there is something else causing the null value. – Jenny B Aug 12 '14 at 16:45
8

Recordtype can be directly queried and you do not have to use SeeAllData for getting record type info

Reference:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

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

Eg:

RecordType rt = [select id,Name from RecordType where SobjectType='Account' and Name='A' Limit 1];

If you want to do it without using a query:

Reference : http://salesforceapexcodecorner.blogspot.com/2012/03/record-type-for-crating-test-class-data.html

Schema.DescribeSObjectResult cfrSchema = Schema.SObjectType.Account; 
Map<String,Schema.RecordTypeInfo> AccountRecordTypeInfo = cfrSchema.getRecordTypeInfosByName(); 
Id rtId = AccountRecordTypeInfo .get('A').getRecordTypeId();

Account Acc = new Account(Name='test',recordtypeid=AccountRecordTypeInfo .get('A').getRecordTypeId());
insert Acc;
| improve this answer | |
  • 2
    note that the doc above is incomplete, it should also include StaticResource which is most definitely available without SeeAllData=true. I have filed a doc feedback on this. – cropredy Aug 12 '14 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.