Saleforce Dynamic Apex

By in
Saleforce Dynamic Apex

Sometimes when you’re developing Web Applications, you need to have picklist values to be shown to the User from the backend. For complex projects, you may even have to filter those picklist values before showing them to the user. In such instances, dynamic apex in Salesforce comes to the rescue.


How to Set Up Dynamic Apex in Salesforce

To understand this, we first need to look at the below code snippet.

// Describe the Account.Name field

Schema.DescribeFieldResult dynamicFieldData = Account.Some_PicklistField__c.getDescribe();

This will give you Label and API Name with some other information regarding the number of bytes for the field.

// Get picklist values from field description

List picklistEntries = Some_PicklistField__c.getPicklistValues();

// Do something with entries

for (Schema.PicklistEntry entry : picklistEntries) {








The traditional SOQL will only give you the data that is stored in the database but using the following way you can get information about the metadata of objects and fields.

Using Dynamic Apex also allows you to check for field/Object permissions at runtime.

In conclusion, it’s very helpful to use dynamic apex in Salesforce when dealing with metadata-level configurations for your organization. You can use fields for an object and then build a SOQL query with them to fetch data.

Continue learning about Apex with some of my other posts, “Ways to Deserialize Apex” and “Apex Unit Testing.

Additional Resources

Cover Photo by Pankaj Patel on Unsplash

Leave a reply

Your email address will not be published. Required fields are marked *