Salesforce Describe Methods – Record Types, Fields, and Global Describe

6 Sep

Record Type Describe

Getting an object’s record type Id is simple with the statement below, and it avoids using a SOQL statment on the RecordType object. However, the method name is misleading because getRecordTypeInfosByName actually retrieves the record type with the Label and not the Name. Also, be careful because the name is case sensitive. A Null Pointer Exception will be thrown if the label does not match exact spelling and case, so add appropriate error handling.

// Return record type Id by DeveloperName 
Id MyRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('My_Dev_Name').getRecordTypeId()

// Return record type Id by Label
Id MyRecTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('My Record Type Label').getRecordTypeId();


Retrieving Record Type information into a map is useful when more than one record type is needed in your logic.

SObjectType ObjType;  
map TypeInfoMap;
Schema.RecordTypeInfo TypeInfo;
Id RecTypeId;

// use Account sObject type as an example
ObjType =  Account.SObjectType
TypeInfoMap = ObjType.getDescribe().getRecordTypeInfosByName();
TypeInfo = TypeInfoMap.get('My Record Type Label');
if (TypeInfo != null) {
    RecTypeId = TypeInfo.getRecordTypeId();
}

sObject Field Describe

This gets a map of field tokens for an sObject type, and then the field describe method can be used to get a lot of useful information about the field. It excellent for making code flexible enough to work with variety of objects like I did with my In-line Editing Visualforce Component.

map FieldMap = Schema.SObjectType.Account.fields.getMap();
for (Schema.SObjectField f : FieldMap.values()) {
    Schema.DescribeFieldResult DescField = f.getDescribe();
    system.debug('Name: ' +  DescField.getName());
    system.debug('Type: ' +  DescField.getType());
}

Here is a link to the Salesforce documentation on all of the field describe result methods.

Global Describe

Global describe returns a map of sObject tokens for all sobjects. Use this to get the sobject type for the describes above and others o.

map GlobalMap = Schema.getGlobalDescribe(); 
for (Schema.SObjectType Obj : GlobalMap.values()) {
    Schema.DescribeSObjectResult ObjDesc = Obj.getDescribe();
    system.debug('Object Name: ' + ObjDesc.getName());
}

Combine Global Describe with describing an Objects fields:

String ObjName1 = 'Account';
String ObjName2 = 'Contact';

// get the map of sObjectType information
map GlobalMap = Schema.getGlobalDescribe(); 

// get the map of fields for Account
map FieldMap1 = GlobalMap.get(ObjName1).getDescribe().fields.getMap();

// get the map of fields for Contact
map FieldMap2 = GlobalMap.get(ObjName2).getDescribe().fields.getMap();

Here is a link to the Salesforce documentation on all of the object describe result methods.

Bounus: SObject Type from a Record Id

The Schema.SObjectType can also be retrieved from a Record Id. It is great when working with Task and Event WhatId fields because they can refer to multiple types of objects. Also, useful for Visualforce Page Controllers that have a record Id passed into the constructor method.

String ObjName = MyId.getSObjectType().getDescribe().getName();
Task MyTask = [Select Id, WhatId from Task where WhatId <> null limit 1];
String ObjName = MyTask.WhatId.getSObjectType().getDescribe().getName();
system.debug(ObjName);
public MyVFPageController(ApexPages.StandardController stdController) {
    Id RecId = strController.getId();
    Schema.DescribeSObjectResult ObjDesc = RecId.getSObjectType().getDescribe();
    map FieldMap = ObjDesc.fields.getMap();
}