Wednesday , March 27 2019
Home / Apex / Dynamic Apex / Salesforce: Dynamic Apex

Salesforce: Dynamic Apex


What is Dynamic Apex?

Dynamic Apex is a way to create a more flexible application with the ability to do the following.

  1. Access sObject and field describe information
  2. Access Salesforce app information
  3. Write dynamic SOQL queries, dynamic SOSL queries, and dynamic DML

Each of the above-mentioned ability will require a post of its own, so let’s begin with the sObject and field describe information.

So, let’s say you want to find what are all the standard and custom object, presently available in an Org, or say you want a select list with all the objects as a select option. Then the sObject describe calls come into the picture.

This call provides all the information necessary to display the object as well as all the fields related to the sObject.

The most important method to begin dynamic apex is the “Schema.getGlobalDescribe() “ method. This method returns you a key value pair of object name as a key and Schema.sObjectType. With the help of this code, we are going to manipulate the rest of the task, syntax of the code is given below.

Public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); // Org Map

Next we are going to use a method:  “Schema.getGlobalDescribe().Values();” this method returns list of sObjectType and will come in handy to make the select list of the objects.

Below is the method which  returns a list of selectoptions, these select options has sObjectLabel as display value and sObjectName as the value.

public list<selectoption> ObjectList {

        get{ return findObjectsInOrg();}


public list<selectoption> findObjectsInOrg(){

        list<selectoption> objectList = new list<selectoption>();

        List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();

        for(Schema.SObjectType f : gd){           

            objectList.add(new SelectOption(f.getDescribe().getname(),f.getDescribe().getLabel()));


        return objectList;


With the help of above method we can make a select list on VisualForce Page.

VisualForce Code Is below :-

<apex:page controller=”WorkbenchTrial” sidebar=”false” showHeader=”true”>

    <apex:form >

        <apex:pageBlock >

            <apex:selectList styleClass=”customStyle” value=”{!selectedObject}” size=”1″ onchange=”rerenderfield()”>

                    <apex:outputLabel styleClass=”customStyle”>Select an object to query :</apex:outputLabel>


                    <apex:selectOptions value=”{!ObjectList}”></apex:selectOptions>






Preview of the page:

sObjects Dynamic




Also, Have a look at the below resources:

  1. Best Salesforce Interview Questions book with Apex and Visualforce concept explained

Also, Have a look at the below learning resources:

  1. SOQL (Salesforce Object Query Language)

  2. Apex Trigger Best Practices and the Trigger Framework

  3. Salesforce Interview Question and Answers Part 2

  4. Salesforce Interview Questions on Test Class

  5. Salesforce-lightning-interview-questions-2018

     6. Salesforce Interview Questions Batch Clas




About Sumit Datta

Sumit Datta
I am a 3x Certified Salesforce developer with overall 5 years of IT experience and 3 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

Check Also

Jitterbit Cloud agent with Salesforce

How to use Jitterbit Cloud agent with Salesforce

How to use Jitterbit Cloud agent with Salesforce     Have you ever found yourself …


How to create a Modal box in Lightning Component?

How to create a Modal box in Lightning Component? In this post we are going …

Salesforce Lightning Training

How to send parameters in AuraEnabled methods in Salesforce Lightning Components?

How to send parameters in AuraEnabled methods in Salesforce Lightning Components? Passing value to and …

Salesforce Lightning Tutorials

How to use lightning accordion

How to use lightning accordion and how to dynamically add accordion section in lightning. In …


  1. Avatar

    thanks for sharing this

  2. Avatar
    Tikam Sangwani

    Hi Sumit,

    Thanks for sharing this article. It is really very helpful. Saved my many hours of research of my project.

Leave a Reply

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