I have a test class which all works perfectly in the Sandbox however when i push to live it fails.

On looking into it i have seen that my RecordTypeID is different in sandbox as it is in production.. Therefore i wondered if i could reference the Record Type name which is the same in both?

I have tried RecordTypeName and RecordType.Name but both throw up errors,

Any ideas>?

enter image description here

4 Answers 4


Assuming that the record type's name is unique, you can get away with not using a describe method at all:

TASKRAY_Project__c project = new TASKRAY_Project__c(
    RecordType = new RecordType(Name='Phase'),

You can also create the RecordType ahead of time and use it as a variable:

RecordType phaseRT = new RecordType(Name='Phase');
TASKRAY_Project__c project = new TASKRAY_Project__c(
    RecordType = phaseRT,

This is great for creating many records of the same record type.

  • Assuming the instantiated RecordType object works the same way as it does in SOQL, you could also specify sObjectType, which is a picklist, or add the DeveloperName if the Name is not unique. I've never used a RecordType as an object before though, so I don't know if this works. Commented Jan 30, 2017 at 13:06
  • 1
    @IllusiveBrian Actually, it only works with indexed fields, unfortunately. I tried DeveloperName before I posted this answer to be sure. As a side note: why isn't DeveloperName an indexed field? sigh
    – sfdcfox
    Commented Jan 30, 2017 at 13:12
  • Ahh, good to know then. Commented Jan 30, 2017 at 13:17

In your code you can retrieve the Record Type Id by using the Schema methods as follows, obviously change the object name for 'Case' and the Developer name for the record type - always use developer names for these as they are more likely to be static.

RecordTypeCaseId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Metering').getRecordTypeId();

You have to use Custom Setting to store information about RecordTypes you need, and then use them in your test class, and you have to use the correct value in each Org (production and sandbox).

When assigning a recordType in your test class, you have to use Id instead of Name.


This is how you can use Custom Setting in your test class :

Imagine the name you give to your custom setting is IdRecordType : the API name will be IdRecordType__c and then in manage button you define instance called 'General' (where you can give values to your variables), so in your code you should put :

 IdRecordType__c confTR = IdRecordType__c.getInstance('General');

  • Hi @ihssan do i write that into the test class or hold it in the controller? And how exactly would i write that as ive never used that before? Commented Jan 30, 2017 at 11:18
  • This is a video tutorial for creating a custom setting link, i will edit my answer to show you how to use it in your test class
    – ihssan
    Commented Jan 30, 2017 at 11:21
  • Thanks all just trying @sfdcfox method now as it seemed quite simple. The test class has passed and i am uploading into production now to test Commented Jan 30, 2017 at 11:25

One method that has the advantage of being dynamic while allowing you to specify an object is to use SOQL. You can query the RecordType object like any other object. In your case, you could use:

RecordTypeID = [select ID from RecordType where Name = 'XXX' and
                            sObjectType = 'TASKRAY__Project__c'].ID
//Or possibly "sObjectType = 'Project__c' and NamespacePrefix = 'TASKRAY'", 
//I have not tried with an object with a namespace

You must log in to answer this question.

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