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: 

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

Sumit Datta

Sumit Datta

I am a 5x Certified Salesforce developer with overall 7 years of IT experience and 5 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.

4 Comments

  1. bhavya July 3, 2017 Reply

    thanks for sharing this

  2. Tikam Sangwani July 3, 2017 Reply

    Hi Sumit,

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

  3. […] Salesforce : Dynamic Apex | SalesforceNextGen. What is Dynamic Apex? Dynamic Apex is a way to create a more flexible application with the ability to do the following. Access sObject and field describe informationAccess Salesforce app informationWrite 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. […]

  4. […] Sourced through Scoop.it from: salesforcenextgen.com […]

Leave a Comment

Your email address will not be published.