Schema Class in Salesforce Apex

In Salesforce Apex, properly organizing database objects, fields, and relationships is required to build easily maintainable applications. For this, we use the Schema Class in Apex, which provides methods to get details about database objects and fields.

In this tutorial, I will explain the Schema class in Salesforce Apex, how it works, and some practical ways you can use its methods.

What is Schema Class in Salesforce Apex?

In Salesforce Apex, the Schema Class is a built-in class that gives us access to metadata information. Using the Schema Class, we can access information about standard and custom objects, their fields, and their relationships in the Salesforce database. With the Schema class, we can describe objects, fields, and relationships without hardcoding them.

When to Use the Schema Class in Salesforce Apex

In Salesforce Apex, the schema class is helpful in scenarios where we need to retrieve information about objects and fields dynamically or when we have to build solutions that update automatically to changes in metadata, such as SOQL queries, Custom Validation rules, and field validation.

Below are the key features of the Apex schema class.

  • Describe Information: The Schema class gives access to metadata for Salesforce objects.
  • Dynamic Querying: We can write dynamic SOQL (Salesforce Object Query Language) queries using Schema methods.
  • Data Model Flexibility: It allows us to build adaptable, scalable solutions without knowing the structure of the data.

Key Components of Schema Class in Apex

In Apex, the Schema namespace contains several useful classes and methods, as mentioned below.

  • Schema.getGlobalDescribe(): This method retrieves a map of all objects in the Salesforce organization, including standard and custom objects.
  • Schema.SObjectType(): It represents a specific object and allows you to retrieve details about the object’s fields, record types, and other information.
  • Schema.SObjectField(): This method represents a field on an object and allows us to access properties such as data type, length, and label.
  • Schema.DescribeSObjectResult(): This function retrieves metadata about an object, such as its name, label, and fields.
  • Schema.DescribeFieldResult(): It provides metadata about a specific field, such as label, type, and whether it’s required.

Use Cases of Schema Classes in Salesforce Apex

Now, we will see the use cases of schema classes mentioned above in Salesforce Apex, with some practical examples.

1. Schema.getGlobalDescribe():

Following the below Apex code, we will retrieve all the objects in the organization using the Schema.getGlobalDescribe() method.

Map<String, Schema.SObjectType> allObjects = Schema.getGlobalDescribe();

for (String objName : allObjects.keySet()) {
    System.debug('Object Name: ' + objName);
}

Output:

get Global describe Schema method in Salesforce Apex

Now, we can see in the output that the schema class method Schema.getGlobalDescribe() retrieved and displayed all the objects in the debug output.

2. Schema.SObjectType():

In the Apex code, we will retrieve all fields of the