+ Start a Discussion
smitrasmitra 

how to display SOQL query result in visualforce page

Hi All,

I have a SOQL query result in controller like this

(Lead:{Name=Bertha Boxer, Phone=(850) 644-4200, Id=00Q9000000EJ3FDEA1, LeadSource=Web, City=Tallahassee}, Lead:{Name=Phyllis Cotton, Phone=(703) 757-1000, Id=00Q9000000EJ3FEEA1, LeadSource=Web}, Lead:{Name=Jeff Glimpse, Phone=886-2-25474189, Id=00Q9000000EJ3FFEA1, LeadSource=Phone Inquiry}

I want to display the result in tabuler format in visual force page.

Please let me know how to display the result 
Arunkumar RArunkumar R
Pass the particular list in pageblocktable ,

for example --> Public List<Acccount> accList{get;set;}

Use query in your constructor if you want to display the values when the page initialize time,

accList = [Select name,phone from Account];

To display the above query result in visualforce page as a tabular format use like below,
<apex:page controller="yourcontrollername">
    <apex:pageBlock title="My Content">
        <apex:pageBlockTable value="{!accList}" var="item">
            <apex:column value="{!item.name}"/>
            <apex:column value="{!item.phone}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


Hope this will helpful to you..! If you need clarification let me know..!

Thanks..

Ramu_SFDCRamu_SFDC
You need to use either pageblock or repeater to display results in VF page. The below post has an example on how to do this

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000952sIAA

gautam_singhgautam_singh

Hi Smitra, 

If you have the query, put that in an list & then you can use the pageblocktable attribute to show the details on Visualforce Page in the form of a Table. 

<!-- Apex: -->

public List<String> getMyLeadList(){

myLeadList = [Your Query];
return myLeadList;
}
List<String>  myLeadList = [Your Query];

<!-- Page: -->

<apex:page Controller="yourcontroller">
    <apex:pageBlock title="My Content">
        <apex:pageBlockTable value="{!myLeadList}" var="item">
            <apex:column value="{!item.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Hope this fulfills your requirement.


Important :

Click on the Star Icon aside if this post provides you with useful information and if this is what you where looking for then please mark it as a solution for others benefits.

Thank You

smitrasmitra
Thanks all for your reply..

Problem here is I want to display in pageblock table but dont know the exact object name (As it is dynamic search it could be account,contact lead or any custom object as well).So the query might return list of Account,Lead case or any custom objects.

Please suggest me how to display for this dynamic entitis

initially i thought of a wrapper class where i selected first 4 attributes of the returnd list after soql query(example name,email,phone) but can u please help with the approach 
Arunkumar RArunkumar R
For your scenario SOSL Query is better to use. Because you don't know the exam object name, the below article might be helpful

http://www.sfdcpoint.com/salesforce/sosl-example-in-salesforce/
smitrasmitra
every example Shown here --number of attributes and name of the attributes are mentioned in the visual force page

for example:

<apex:pageblockTable value="{!conList}" var="con">
      <apex:column value="{!con.name}"/>
      <apex:column value="{!con.email}"/>
</apex:pageblockTable>

But the problem is when i want to render the search result in vf page it could be anything..its not a predetermined set of attributed that I want to display..There should be some generic holder which will keep track of the attributes that i want to display..considering my example..if it is lead {Name=Bertha Boxer, Phone=(850) 644-4200, Id=00Q9000000EJ3FDEA1, LeadSource=Web, City=Tallahassee},

Then in visula force page it should appear like

Name|Phone|Id|LeadSource|City
Bertha|(850) 644-4200|Web|Tallahassee


gautam_singhgautam_singh
Hi Smitra

What I assume as your problem is - 

You have a query that can result many records from different object & you need to show them on a Visualforce Page. Try understanding the usability in this snippet of code. It would help. 

If you nee to add more functionalities to the search results, the best would be bob's blog here (http://bobbuzzard.blogspot.co.uk/2011/06/execute-custom-search-when-opening-page.html),
public class opportunityList2Con {
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([select name,closedate from Opportunity]));
            }
            return setCon;
        }
        set;
    }
    public List<Opportunity> getOpportunities() {
         return (List<Opportunity>) setCon.getRecords();
    }
}

<apex:page controller="opportunityList2Con">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!opportunities}" var="o">
            <apex:column value="{!o.name}"/>
            <apex:column value="{!o.closedate}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Hope this fulfills your requirement.


Important :

Click on the Star Icon aside if this post provides you with useful information and if this is what you where looking for then please mark it as a solution for others benefits.

Thank You