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.