Monday, June 8, 2015

How to use Salesforce Describe Methods – Record Types, Fields, and Global Describe

 In this post, I will try to explain how to use Salesforce Describe Methods to fetch record types, fields etc.

Record Type Describe -
If you want to fetch an object's RecordType ID, you don't need to do SOQL operation. There is a method called getRecordTypeInfosByName is there for you. Don't go by the name of the method, as this method will return you the record type id searching by label, not by name. The label is case sensitive. So if the label does not match the exact spelling and case, a null pointer exception will be thrown. So as a developer, you need to add proper error handling mechanism.

When you have more than one record type then you can put the record type information into a map. Below is the code to do that -

sObject Field Describe -
This will return a map of field tokens for an sObject type and then the field describe method can provide a number of useful information about that field. With this, you can make your code flexible to work for any object. Below is the code -

You can refer to the Salesforce Documentation for all the methods . Click Here

Global Describe -
This will return a map of sObject tokens for all sObjects. Below is the method -

Note - Fetch sObject Type from Record Id
The Schema.SObjectType can be retrieved from a Record Id. This is really helpful when you are working with Task or Event. The reason is WhatId field can refer to multiple types of objects. By fetching sObject type from WhatId field, dynamically you can decide your business logic.
Here is the code snippet -



0 comments:

Post a Comment