Is there a statically accessible SObjectType for RecordType?

System.debug(Account.SObjectType); // Result: Account
System.debug(RecordType.SObjectType); // Result: SObjectType

RecordType class explains why RecordType.SObjectType is different.

I know I can get the Type from an Id:

Id rtId = '012C0000000UrhQ';
System.debug(LoggingLevel.ERROR, rtId.getSObjectType()); // RecordType

But is there also a static accessor, like for all other types?

  • 1
    Everything works as expected. It looks strange but you have two different expressions. 1. RecordType.SObjectType indeed evaluates to a Schema.SObjectField because there is a Field SObjectType on RecordType. 2. Account.SObjectType indeed evaluates the specific SObjectType Account – Robert Sösemann Oct 12 '17 at 10:55
  • I rewrote the question, thanks for your help @RobertSösemann – Basti Oct 12 '17 at 11:09

You can get the describe:

DescribeSObjectResult describe = SObjectType.RecordType;

From the describe, you can then use the getSObjectType() method:

SObjectType schemaType = SObjectType.RecordType.getSObjectType();

As noted in the comments, in this case you retrieve the SObjectField with this name where you would normally return the SObjectType:

SObjectField someField = RecordType.DeveloperName;
SObjectField typeField = RecordType.SObjectType;

You might run into the same issue on other objects with a field so named, such as:

  • FieldPermissions
  • ObjectPermissions
  • QueueSobject
  • etc.

You can call getSObjectType() as a static method on any concrete sObject types. It's resolved at run time rather than compile time (which is what I'm assuming you actually mean by static), but probably fits your purpose:

System.debug(RecordType.getSObjectType());//Result: RecordType
  • I can't wrap my head around why this version doesn't throw a compile error. It should say Variable does not exist: RecordType, but when I run it through Execute Anonymous, I get no error. Weird... – Adrian Larson Oct 12 '17 at 18:58
  • @AdrianLarson Best I can tell, using the "RecordType" identifier puts the compiler in "sObject Land", where the rules are different. Similarly, RecordType.class.sObjectType is the same as RecordType.sObjectType, but Type.ForName('RecordType').sObjectType is invalid, even though RecordType.class.equals(Type.ForName('RecordType')) is true. – IllusiveBrian Oct 12 '17 at 20:09

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.