Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home1/oijoiv2f/public_html/wp-content/themes/entaro/template-posts/single/inner.php on line 23

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: 

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. […] // Org Map public list<selectoption> ObjectList { get{ return findObjectsInOrg();} Salesforce Dynamic Apex sobjectfield | SalesforceNextGen. Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically […]

  2. manish July 4, 2017 Reply

    Where can I find the complete code?

  3. […] Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !! […]

Leave a Comment

Your email address will not be published.