function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Swamy PSwamy P 

displaying fields in PageBlockTable by Dynamic soql queries

Hi frndz,

                I have a scenario i.e, iam querying a records by Dynamic SOQL with retun type list<sobject>.I want to display thus retrieved fields in a PageBlock Table for that object...How can i acheive this..

Class:

    public PageReference pbrecords() {
    
    List<sobject> recs=Database.query('Select id,Name from '+sobj+' Where Name =:'+name);
        selected_Objrecs =new list<sobject>();
        for(sobject s:recs){
        
            selected_Objrecs.add(s);
        }
        return null;
    }

  public list<sobject> selected_Objrecs { get; set; }

----------------------

V.F:

<apex:pageBlockTable value="{!selected_Objrecs}" var="a">
                  <apex:column value="{!a}" />
    </apex:pageBlockTable>

kiranmutturukiranmutturu
you can make use of like this

<apex:column value="{!a.id}" />

<apex:column value="{!a.name}" />
Shiv ShankarShiv Shankar

You can write this way 

<apex:pageBlockTable value="{!selected_Objrecs}" var="a">
                  <apex:column value="{!a.id}" />

                   <apex:column value="{! a.Name}" />
 </apex:pageBlockTable>

 

 

Quetion from my side

Why have you written return type pageReference for pbrecords ? you are returning null why ?

Swamy PSwamy P

Hello Mr.shiv and Kiran,

                                           You people are giving that {!a.id} as well {!a.Name} but here am getting an error ::::

Error: Unknown property 'SObject.name'

  This is because am returning list<Sobject> ...so once check it...

Adam HowarthAdam Howarth

You could make use of dynamic components.

 

You build the table in the controller so you can determine the types and assign them to the columns.

 

Here is an example:

 

Page:

 

<apex:page controller="YourController"
 <head>
 </head>
<body>
<h1>
Page Title
</h1> <apex:dynamicComponent componentValue="{!getTable}" /> </body>
</apex:page>

 

Controller:

 

// Generates the data table for the 	
public Component.Apex.PageBlockTable getTable()

{
	Component.Apex.PageblockTable table = new Component.Apex.PageblockTable();
	table.value= selected_Objrecs;
	table.var = 'genObj';
		

	// If you need to determine the type, then do it here.

         if(selected_Objrecs[0].getType() == yourCustomObject__c)
         {
      
	     Component.Apex.Column firstNameCol = new Component.Apex.Column();
	     firstNameCol.value = '{!genObj.FieldA}';

	     table.childComponents.add(firstNameCol);
         }
         else
         {
              Component.Apex.Column secondNameCol = new Component.Apex.Column();
	     firstNameCol.value = '{!genObj.FieldB}';

	     table.childComponents.add(firstNameCol);

         }
	return table;
}

 

Let me know if you need more help.

 

Cheers,

Adam

Swamy PSwamy P

Hello Adam,

                       the reply from ur side  is for fixed values i.e,Either first name or email like that..but my intension is that when i change the object then automatically change the columns also..

 

this is my code:

<apex:page controller="Dynamic_search" sidebar="false">
  <apex:form id="f">
      <apex:pageBlock title="Records from Diff Objects" tabStyle="Contact">
          <apex:pageBlockSection title="Search By Name" collapsible="false">
            <apex:outputPanel layout="block" style="margin-left:150px;">
              <apex:outputLabel value="Name:" style="padding-top:3px;float:left;"/>
              <apex:inputText value="{!name}" style="width:180px;float:left;"/>
              <apex:commandButton action="{!Search}" value="Search" style="float:left;" reRender="f"/><br/>
            </apex:outputPanel>
          </apex:pageBlockSection>
          <apex:pageBlockSection title="Select Objects for Getting Records" collapsible="false" columns="2">
               
              <apex:selectCheckboxes value="{!objects}" layout="pageDirection" style="float:left"  >
                  <apex:selectOptions value="{!allobjs}"/>
                  <apex:actionSupport event="onclick"  action="{!pbrecords}"/>
              </apex:selectCheckboxes>
              <apex:pageBlockTable value="{!selected_Objrecs}" var="a">
                  <apex:column value="{!a.id}" />
                  <!--<apex:column value="{!a.name}"/>-->
              </apex:pageBlockTable>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

---

class:

 

public class Dynamic_search {


 public string objects { get; set; }
    
 List<SelectOption> sloptn=new List<SelectOption>();
    public list<SelectOption> getAllobjs() {
            
        return sloptn;
   }
             Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
             Schema.sobjectType sobj=schemaMap.get(objects);
 
    public PageReference Search()
                 {      
             list<schema.SobjectType> lst=schemaMap.values();
                  for(schema.SobjectType ss:lst){
                      sloptn.add(new selectoption(ss.getDescribe().getLocalName(),ss.getDescribe().getLabel()));
                      sloptn.sort();
                  }               
        return null;
    }

    public String name { get; set; }
      
      public list<sobject> selected_Objrecs { get; set; }

    public PageReference pbrecords() {
    
    List<sobject> recs=Database.query('Select id,Name from '+sobj+' where Name=:'+name);
        selected_Objrecs =new list<sobject>();
        for(sobject s:recs){
        
            selected_Objrecs.add(s);
        }
        return null;
    }


}