+ Start a Discussion
gaga SFDCgaga SFDC 

how to display records on visualforce page using dynamic query ?

 query = 'Select ' + CorrespondingFields + ' from ' + AllObjectsList;

Here,  object and fields of that object both are dynamic.
pls help how to display records in table on visualforce page.
Best Answer chosen by gaga SFDC
Ashish DevAshish Dev
Well, it made me happy to write some dynamic vf stuff after long.:)
You can use below page and controller as a reference.

Page:
<apex:page controller="DF_PageController">
    <apex:form>
    	<apex:pageblock>
        	<apex:pageBlockTable value="{!lstObjs}" var="rec">
            	<apex:repeat value="{!lstFlds}" var="fld">
                	<apex:column value="{!rec[fld]}"/>
                </apex:repeat>
            </apex:pageBlockTable>        
        </apex:pageblock>
    </apex:form>
</apex:page>

Controller:
public class DF_PageController {
    public List<sObject> lstObjs {get;set;}
    public List<string> lstFlds {get;private set;}
    
    public DF_PageController(){
        lstFlds = new List<String>();
		string query = 'select id, name, AccountNumber  from Account limit 50';
        lstObjs = Database.query(query);
        
        Set<string> setFlds = new Set<String>();
        for(integer i=0;i<lstObjs.size();i++){
            setFlds.addAll(lstObjs[i].getPopulatedFieldsAsMap().keySet());
        }
        
        lstFlds.addAll(setFlds);
        system.debug(lstFlds);
    }
}

Let me know if this helps you.

All Answers

Ashish DevAshish Dev
I would recomend you to read documentation in detal https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm
Basically you need to
1) prepare query string
2) execute that query using database.query(dynamicQuery)
3) use returned records in vf page like in pageblock table for showing in tabular format. 
gaga SFDCgaga SFDC
Hi Ashish,
i have done these step alerady like

query = 'Select ' + CorrespondingFields + ' from ' + AllObjectsList;
list<sObject> slist = database.query(query);

but only problem is that i am not able to display it on the vf page, dont know what should i write on value tags to display records as the object and fields are dynamic

<apex:PageBlockTable value=" "  var="dynamicfields">
       <apex:column value=" " />
</apex:PageBlockTable>

 
Ashish DevAshish Dev
Well, it made me happy to write some dynamic vf stuff after long.:)
You can use below page and controller as a reference.

Page:
<apex:page controller="DF_PageController">
    <apex:form>
    	<apex:pageblock>
        	<apex:pageBlockTable value="{!lstObjs}" var="rec">
            	<apex:repeat value="{!lstFlds}" var="fld">
                	<apex:column value="{!rec[fld]}"/>
                </apex:repeat>
            </apex:pageBlockTable>        
        </apex:pageblock>
    </apex:form>
</apex:page>

Controller:
public class DF_PageController {
    public List<sObject> lstObjs {get;set;}
    public List<string> lstFlds {get;private set;}
    
    public DF_PageController(){
        lstFlds = new List<String>();
		string query = 'select id, name, AccountNumber  from Account limit 50';
        lstObjs = Database.query(query);
        
        Set<string> setFlds = new Set<String>();
        for(integer i=0;i<lstObjs.size();i++){
            setFlds.addAll(lstObjs[i].getPopulatedFieldsAsMap().keySet());
        }
        
        lstFlds.addAll(setFlds);
        system.debug(lstFlds);
    }
}

Let me know if this helps you.
This was selected as the best answer
gaga SFDCgaga SFDC
Thanks bro :-)
Krishnan MishraKrishnan Mishra
In my case it is showing following error: 
Cannot convert the value of '{!fieldNames}' to the expected type.

public class PaginationForComponent {
    public String objName{get;set;}
	public String[] fieldNames{get;set;}
	list<sObject> con = new list<sObject>();

	
	public PaginationForComponent(){
		RecordsPerPageslist = 10;
	}

	public String query;
	public String alphaSearchConct{get;set;}
	public string msg {get;set;}
	public Map<id,Boolean> m = new Map<id,boolean>(); 
	list<sObject> sortedList;
	public String myOrder{get;set;}                 // Ascending or Descending order of sorting
	public String sortField{get;set;}               // Field by which sorting should be done
	public boolean selectAll{get;set;}
	public list<String> alphabet{get;set;}
	public list<sObject> cont;
	public list<wrapper> allContactList = new list<wrapper>();
	public void fieldvalues(){
		query = fieldNames[0] + ' , ';
		for(Integer i=1;i<fieldNames.size();i++){
			query = query + fieldNames[i] + ' , ';
		}
		query='SELECT ' + query + ' FROM ' + 'objName';
	}
	public Integer RecordsPerPageslist{ 
        get;
        set{                                                          //To select number of records per page
            if(value!=null){
                this.RecordsPerPagesList=value;
                System.debug('RecordsPerPageList called');
            }
        }       
    }  
    public ApexPages.StandardSetController stdSetController{            //Instantiating a standard set controller
          get{
              if(stdSetController==null){
                    con = Database.query(query);
                    System.debug('con in ssc is : ' + con);
                   stdSetController = new ApexPages.StandardSetController(con);
              }
                stdSetController.setPageSize(RecordsPerPageslist);        //Limiting Number of records to be displayed per page 
                  System.debug('stdSetController called ');
              return stdSetController;   
          }
          set;
      }
    public list<wrapper> getWrapperContacts(){      //List of wrapper class to display in table
       for(sObject c:(list<sObject>)stdSetController.getRecords()){
       		allContactList.add(new wrapper(c));
       }
        return allContactList;
    }

    public class wrapper{
	  public boolean isSelected{get;set;}  
	  public sObject con{get;set;}  
	  public   wrapper(sObject con){
	          System.debug('constructor of wrapper class called ');
	          isSelected = false;
	          this.con = con;
	          }
	          
	      }
}
 
<apex:component controller="PaginationForComponent" >
    <apex:attribute name="objectName" description="The object's name" type="String" required="true" assignTo="{!objName}"/>
    <apex:attribute name="fieldName" description="Fields to be displayed" type="String[]" required="true" assignTo="{!fieldNames}"/>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!WrapperContacts}" var="contacts">
            <apex:repeat value="{!fieldNames}" var="fldNames">
            <apex:column value="{!contacts.con[fldNames]}"/> 
        </apex:repeat>
        </apex:pageBlockTable>
        
    
    </apex:pageBlock>
</apex:component>