Dynamic Apex in Salesforce: A Beginner-Friendly Guide

Dynamic Apex is a powerful concept in Salesforce that enables developers to interact with metadata (like objects, fields, and permissions) at runtime. This means that instead of hardcoding specific fields or objects, you can access them flexibly, making your code more adaptable to changes. For students and beginners in Salesforce development, understanding Dynamic Apex is essential, as it offers a deeper understanding of Salesforce’s data and metadata structures.

This guide will introduce Dynamic Apex and explain some key classes, with easy-to-understand examples to help you get started.


What is Dynamic Apex?

In Salesforce, Dynamic Apex refers to the ability to access and work with metadata—such as objects, fields, and picklists—dynamically. Think of it as a way to interact with Salesforce’s data model on the fly, allowing you to access or modify objects and fields even if you don’t know their names in advance.

Benefits of Dynamic Apex

  • Flexibility: Enables you to create adaptable, future-proof code.
  • Reduced Maintenance: When objects or fields change, Dynamic Apex allows your code to automatically adapt.
  • Enhanced Metadata Access: Lets you interact with Salesforce’s schema (the structure of data) to check user permissions, field values, and more.

Key Classes in Dynamic Apex

Here are some important classes in Dynamic Apex that provide access to various aspects of the Salesforce schema:

  1. Schema Class: The main class for accessing metadata in Salesforce.
  2. Schema.SObjectType: Retrieves details about specific Salesforce objects.
  3. Schema.SObjectField: Accesses information about specific fields on an object.
  4. Schema.DescribeSObjectResult: Holds details about an object, including its fields and record types.
  5. Schema.DescribeFieldResult: Provides information about a field, like its data type and picklist values.
  6. Schema.PicklistEntry: Allows access to individual picklist values on a field.
  7. Schema.RecordTypeInfo: Provides information about different record types for an object.

Example 1: Retrieving All Object Names in Salesforce

Using Dynamic Apex, you can retrieve the names of all objects in your Salesforce organization. Here’s a simple code example:

Map<String, Schema.SObjectType> allObjects = Schema.getGlobalDescribe();
for (String objectName : allObjects.keySet()) {
    System.debug('Object Name: ' + objectName);
}
  • Schema.getGlobalDescribe() fetches a map of all objects.
  • We then loop through and print each object’s name.

Example 2: Accessing Record Types Dynamically

In Salesforce, objects can have different record types. To access these record types dynamically, we use the DescribeSObjectResult and RecordTypeInfo classes.

Schema.DescribeSObjectResult describeResult = Account.SObjectType.getDescribe();
List<Schema.RecordTypeInfo> recordTypes = describeResult.getRecordTypeInfos();

for (Schema.RecordTypeInfo recordType : recordTypes) {
    System.debug('Record Type: ' + recordType.getName());
}

Explanation:

  • Account.SObjectType.getDescribe() gives us metadata about the Account object.
  • getRecordTypeInfos() returns all record types for that object, and we print each one’s name.

Example 3: Accessing Fields of Objects Dynamically

To access fields on an object dynamically, you can use Schema.DescribeSObjectResult and Schema.SObjectField.

Schema.DescribeSObjectResult describeResult = Account.SObjectType.getDescribe();
Map<String, Schema.SObjectField> fields = describeResult.fields.getMap();

for (String fieldName : fields.keySet()) {
    System.debug('Field Name: ' + fieldName);
}

Explanation:

  • fields.getMap() provides a map of all fields on the Account object.
  • We then print each field’s name, giving us access to all fields dynamically.

Example 4: Accessing Picklist Values Dynamically

To retrieve picklist values for a specific field, such as the Industry field on the Account object, use Schema.DescribeFieldResult and Schema.PicklistEntry.

Schema.DescribeFieldResult industryField = Account.Industry.getDescribe();
List<Schema.PicklistEntry> picklistValues = industryField.getPicklistValues();

for (Schema.PicklistEntry picklistValue : picklistValues) {
    System.debug('Picklist Value: ' + picklistValue.getLabel());
}

In this code:

  • We get metadata about the Industry field.
  • We then print each picklist value, allowing us to dynamically access all available values.

Example 5: Checking Object Permissions Dynamically

You can check a user’s permissions on an object to see if they can create, read, update, or delete records. Here’s an example for the Account object:

Schema.DescribeSObjectResult describeResult = Account.SObjectType.getDescribe();

System.debug('Createable: ' + describeResult.isCreateable());
System.debug('Updateable: ' + describeResult.isUpdateable());
System.debug('Deleteable: ' + describeResult.isDeletable());
System.debug('Readable: ' + describeResult.isAccessible());

Explanation:

  • isCreateable(), isUpdateable(), isDeletable(), and isAccessible() are methods that return whether a user can perform specific actions on the Account object.

Example 6: Checking Field Permissions Dynamically

Similarly, you can check a user’s permissions on a specific field within an object, like the Industry field on the Account object.

Schema.DescribeFieldResult fieldDescribe = Account.Industry.getDescribe();

System.debug('Field Createable: ' + fieldDescribe.isCreateable());
System.debug('Field Updateable: ' + fieldDescribe.isUpdateable());
System.debug('Field Accessible: ' + fieldDescribe.isAccessible());

Explanation:

  • This code verifies if the user has permission to create, update, or read the Industry field on the Account object.

Conclusion

Dynamic Apex in Salesforce enables developers to interact with metadata and create flexible, adaptable code that responds to changes in Salesforce’s data structure. By understanding and using key classes like Schema, Schema.SObjectType, Schema.SObjectField, and others, you can manage objects, fields, permissions, and metadata dynamically, creating smarter and more resilient applications.

Whether you’re accessing record types, picklist values, or checking permissions, Dynamic Apex offers a wide range of possibilities to explore in Salesforce.