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

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/switch/field_switch.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/switch/field_switch.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/media/field_media.php on line 46

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/media/field_media.php on line 46

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/select/field_select.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/select/field_select.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/editor/field_editor.php on line 46

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/editor/field_editor.php on line 46

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/text/field_text.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/text/field_text.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/slider/field_slider.php on line 40

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/slider/field_slider.php on line 40

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/radio/field_radio.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/radio/field_radio.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/info/field_info.php on line 45

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/info/field_info.php on line 45

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/typography/field_typography.php on line 50

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/typography/field_typography.php on line 50
Salesforce: Dynamic SOQL Chapter 3 contd.

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 SOQL Chapter 3 contd.

Salesforce: Dynamic SOQL Chapter 3 Contd.

Now in the previous post, we were able to fetch the fields related to an sObject at the run time and we used string o create a structured query according to the SOQL syntax. Below is the complete code where I have selected an sObject at run time and found its field and fetched a list of sObject.

When it comes to displaying this list of sObject on the Visualforce page, then it becomes little tricky. As we know that to print on the Visualforce page we need to know the field of the sObject before hand, therefore, the regular way of displaying the table is out of the question.

To overcome this issue, we created a list of string as well while we created the query string for the database.query() method. With help of this, we were able to display all the fields fetched at the run time. As we find in SQL.

Below is the complete code :-

Apex:

public class PostTestingClass {   

    public list<sObject>sObjectList {get;set;}

    public list<string> FieldNameForTable1{get;set;}

    public list<string> FieldNameForTable{get;set;}

    public list<selectoption> fieldList {

        get{return findTheFields();}

        set;}

    public boolean resultTableVisible {get;set;}

    public string selectedObject = ‘sfng__ob1__c’;

            public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); // Org Map

     public string queryString {

        get{

            FieldNameForTable = new list<string>();

            string finalQueryString = ‘select ‘ ;

            for(SelectOption s:fieldList){

                finalQueryString = finalQueryString + s.getValue() + ‘,’;

                FieldNameForTable.add(s.getValue());

            }

            finalQueryString = finalQueryString.removeEnd(‘,’);

            finalQueryString += ‘ from ‘ + selectedObject;          

            return finalQueryString ;}

        set;

    } 

    public  PostTestingClass(){resultTableVisible = false;}

    public list<selectoption> findTheFields(){

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

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

               

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

                        schema.describefieldresult dfield = sfield.getDescribe();                       

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

                }

        return fieldList1;

    }

        public void fetchResultOfQuery(){

            resultTableVisible = true;

        sObjectList = new list<sObject>();

        FieldNameForTable1 = new list<string>();      

      

        system.debug(‘queryString — > ‘ + queryString);

        sObjectList = database.query(queryString);       

        for(string s: FieldNameForTable){

            s = s.replace(‘ ‘, ”);

            FieldNameForTable1.add(s);

        }

                        system.debug(‘FieldNameForTable1 — > ‘ + FieldNameForTable1);       

    }           

}

Visual Force Code:-

<apex:page controller=”PostTestingClass” >

    <apex:form >

        <apex:pageBlock >

            <apex:selectList size=”1″>

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

            </apex:selectList><br/><br/><br/>

            <apex:inputTextarea value=”{!queryString}” /><br/>

            <apex:commandButton action=”{!fetchResultOfQuery}” value=”Search” />

        </apex:pageBlock>

         <apex:pageBlock id=”resultTable” rendered=”{!resultTableVisible}”>           

            <apex:outputPanel >

                <apex:pageBlockTable value=”{!sObjectList}” var=”records”>

                    <apex:repeat value=”{!FieldNameForTable1}” var=”fields”>

                        <apex:column value=”{!records[fields]}”></apex:column>

                    </apex:repeat>

                </apex:pageBlockTable>

            </apex:outputPanel>

        </apex:pageBlock>

    </apex:form>

</apex:page>

Dynamic SOQL result

Next, I will show you how to implement all of this in a small project which works just as SOQL Query builder of WorkBench.

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.

6 Comments

  1. bhavya July 5, 2017 Reply

    Thanks for sharing this article. It is really very helpful.

  2. […] Salesforce: Dynamic SOQL Chapter 3 contd. | SalesforceNextGen. Salesforce: Dynamic SOQL Chapter 3 Contd. Now in the previous post, we were able to fetch the fields related to an sObject at the run time and we used string o create a structured query according to the SOQL syntax. Below is the complete code where I have selected an sObject at run time and found its field and fetched a list of sObject. When it comes to displaying this list of sObject on the Visualforce page, then it becomes little tricky. As we know that to print on the Visualforce page we need to know the field of the sObject before hand, therefore, the regular way of displaying the table is out of the question. To overcome this issue, we created a list of string as well while we created the query string for the database.query() method. […]

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

  4. […] Salesforce: Dynamic SOQL Chapter 3 contd. […]

  5. Rahul July 5, 2017 Reply

    Hi there,

    Could you please explain what are replacing and doing here . I am unable to understand this part

    for(string s: FieldNameForTable){

    s = s.replace(‘ ‘, ”);

    FieldNameForTable1.add(s);

    }

    • Sumit Datta
      Sumit Datta July 5, 2017 Reply

      With the help of this code I am removing space at the end of every field name

Leave a Comment

Your email address will not be published.