You need to sign in to do that
Don't have an account?
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>
<apex:column value="{!a.id}" />
<apex:column value="{!a.name}" />
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 ?
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...
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:
Controller:
Let me know if you need more help.
Cheers,
Adam
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;
}
}