Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/feed-rss2.php on line 8
sobjectfields Archives - Salesforce Next Gen https://salesforcenextgen.com/tag/sobjectfields/ Trailhead and Beyond Mon, 28 Dec 2020 19:46:37 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 https://salesforcenextgen.com/wp-content/uploads/2020/10/cropped-76dc0dd6-326a-4956-a412-bfdf20c7fb23_200x200-32x32.png sobjectfields Archives - Salesforce Next Gen https://salesforcenextgen.com/tag/sobjectfields/ 32 32 Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !! https://salesforcenextgen.com/salesforce-dynamic-apex-chapter-2-fetching-fields-of-sobject-dynamically/ https://salesforcenextgen.com/salesforce-dynamic-apex-chapter-2-fetching-fields-of-sobject-dynamically/#comments Tue, 04 Jul 2017 04:07:27 +0000 http://salesforcenextgen.com/?p=725 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 …
Continue reading Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !!

The post Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !! appeared first on Salesforce Next Gen.

]]>
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: 

The post Salesforce Dynamic Apex Chapter 2 Fetching Fields of sObject Dynamically !! appeared first on Salesforce Next Gen.

]]>
https://salesforcenextgen.com/salesforce-dynamic-apex-chapter-2-fetching-fields-of-sobject-dynamically/feed/ 4