Quantcast
Monday , January 21 2019
Home / Apex / Dynamic Apex / Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !!

Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !!

Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !!

In the previous post, we were able to fetch all the sObjects which are present in the Org at that time. So now the obvious question is, can we fetch the fields of a sObject dynamically as well? yes, indeed we can. Salesforce has provided us with the SObjectField Class and Schema.DescribeFieldResult class. These classes provide us access to the metadata of a specific sObject’s fields at run time. With the help of these classes, we not only get information about name or label, we get a lot more information for e.g. whether the field is accessible or is it a calculated field etc.

Below I will show how to fetch fields of a sObject dynamically. Here I am going to fetch all the fields available for a specific sObject and show it as a select list on a Visualforce page.

First method that I am going to use is “schemaMap.get(selectedObject).getDescribe().fields.getMap();” this method returns a map of String and Schema.SObjectField. using this returned map we make use of a for loop where we traverse through each field and make a call to describe the method, which returns us with the schema describe field result variable. This variable holds all the values we require to make the select list.

Below is the apex code:

public list<selectoption> findObjectsInOrgSelectList(){

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

        if((string.isNotBlank(selectedObject)) || (string.isNotEmpty(selectedObject))){           

                if(schemaMap.get(selectedObject) != schemaMap.get(”)){

                    Map<String, Schema.SObjectField> schemaFieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();

               

                    for(Schema.SObjectField sfield : schemaFieldMap.Values()){                       

                        schema.describefieldresult dfield = sfield.getDescribe();                       

                        fieldList.add(new SelectOption(dfield.getname(),dfield.getlabel()));

                }

            }

        }       

        return fieldList;

    }

The method “findObjectsInOrgSelectList” returns a list of selectoption populated with all the fields related to a selected sObject present in the Org dynamically.

Following is the property in Apex class which uses this method and is accessible on visual force page.

public list<selectoption> fieldList {

        get{ return findObjectsInOrgSelectList();}

        set;

    }

With help of this method and property we make the select list in the Visual force page as shown below:-

<apex:page>                                      

            <apex:form>

                        <apex:pageBlock>

                                    <apex:outputPanel>

                                                <apex:selectList size=”6″ multiselect=”true” value=”{!selectedfield}” style=”width:150px;”

                                 onchange=”rerenderQueryString()”  >                 

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

                    <apex:actionfunction name=”rerenderQueryString” reRender=”queryString” />

                        </apex:selectList>

                                    </apex:outputPanel>

                        </apex:pageBlock>

            </apex:form>

</apex:page>

sObjectField dynamic

Here we see that on the selection of a custom object the related fields of the object are populated in the select list.

 

**Note this code is in continuation of the previous post, I will merge all the code to make a complete working model of Dynamic Apex.

 

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 …

Leave a Reply

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