Quantcast
Monday , January 21 2019
Home / Apex / Dynamic Apex / Salesforce: Dynamic Apex

Salesforce: Dynamic Apex

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();}

        set;}

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>

                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

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

</apex:selectList>

  </apex:pageBlock>

    </apex:form>

</apex:page>

 

Preview of the page:

sObjects Dynamic

 

Next: 

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

MODAL Box

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 …

Salesforce Lightning Tutorial

How to fetch and display list of records in Lightning Component

How to fetch and display list of records in Lightning Component? In this post we …

4 comments

  1. thanks for sharing this

  2. 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 *