How to access Salesforce Data Model via Apex?

In this blog post, we will see how to access Salesforce Data Model via Apex. The describeSObjects Schema method, tokens and describe results can be utilized for describing sObjects programmatically. Metadata information, such as a list of top-level standard and custom objects and their fields can be retrieved.

  • Describe information provides metadata about sObject and field properties.
  • Information related to operations supported by sObjects, such as create or undelete, can be returned.
  • Information such as the sObject’s name, label, child objects, and fields can be returned.
  • Describe information provides information about objects and not records of the objects.
  • There are two data structures and a method available for sObject and field describe information:
    Token
    Describe result
    The describeSObjects method

Tokens:

Tokens are lightweight reference to an sObject or field which make code faster and more efficient. Both sObjects and fields can be described using tokens.

  • The getSObjectType method can be used to access the token for an object.
  • The getSObjectField method can be used to access the token for a field.
  • The sObjectType member variable can also be used on an sObject Type, such as Account, to access the token for the sObject.
  • The static member variable name of an sObject static type, such as Account.Name, can be used to access the token for the related field. The getDescribe method is used to return the describe result for the token.

Schema Method:

  • The describeSObjects Schema method can be used to describe sObjects.
  • The describeSObjects Schema method can be called and passed one or more sObject type names.
  • An object of the DescribeSObjectResult class can be used to store the results of the obtained describe information.

Describe Result

  • Methods available in the DescribeSObjectResult class can be used for describing sObjects.
  • Methods available in the DescribeFieldResult class can be used for describing sObject fields.
  • The describe result for an sObject or field can be accessed by calling the getDescribe method.
  • The Schema sObjectType static variable with the name of the sObject can also be used to access the describe result.

Schema Namespace Classes

A number of important classes are provided by the Schema namespace for Schema metadata information:

DescribeSobjectResult – Contains methods for describing sObjects
DescribeFieldResult – Contains methods for describing sObject fields
DescribeTabSetResult – Contains methods for obtaining metadata information about a
standard or custom app
DescribeTabResult– Contains methods for obtaining metadata information about a tab in a standard or custom app
RecordTypeInfo – Contains methods for obtaining metadata information about record types of an object
SObjectType – An object of this class is returned from the field describe result using the getReferenceTo() method, or from the sObject describe result using the getSobjectType() method
SObjectField – An object of this class is returned from the field describe result using the getController() and getSObjectField() methods.

To Learn more: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_describesobject.htm

Leave a Reply

Close Menu