+ Start a Discussion
Trudy DobbelaereTrudy Dobbelaere 

How to display SOQL results in Visualforce page

I need to display the search results of a SOQL query in a Visualforce page. The results should be displayed in a list (e.g. 7 Contacts are returned in the search results - the list would have 7 rows). Can I do this without a controller class? I already have the search results. I do not need to run another query. I simply need a Visualforce page to display the results. If I have to use a controller class is there a way to pass the search results (i.e. list of objects) into the controller via the ApexPages.currentPage().getParameters() method or by some other means? Or is there another way to display the results without even using the Visualforce page?

PratikPratik (Salesforce Developers) 
Hi Trudy,

To Store the results/records return by SOQL query, you will need c controller.

You can refer:



P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
Abilash Kosigi 8Abilash Kosigi 8
Hi Trudy,

You need a controller class to pass values to Visualforce page. You can use a Pageblock table in the VF Page the display the list of objects.
The List<SObjects> must be defined in the controller class and the query results must be passed to this list. Using this list values, the page block table must be created in the VF page. There is no need for ApexPages.currentPage().getParameters() method.

Hi Trudy, 

It's really simple, you need to have a list<Object> defined in Apex Controller, like list<Account> accts = [select id, Name from Account limit 30] ;

and then in vf page you may use, <apex:pageBlockTable> , <apex:repeat> 

<apex:pageBlockTable value="{!accts}" var="item">
         <apex:column value="{!item.id}"/> 
         <apex:column value="{!item.name}"/>

please mark this answer, if it resolves your query.

Trudy DobbelaereTrudy Dobbelaere
Thanks for the feedback. So it sounds like I have to use a controller. What about my question about not having to redo the query? Most of your responses referenced a query. I already know the results. I do not want to run another query. I would prefer to simply pass the matching results into the controller like the sample below. However, I am getting an illegal assignment error when I try to do this. 
List <Contact> results = ApexPages.currentPage().getParameters().get('contacts'); 
Trudy, It's because you are assinging a String to a List variable. "
 ApexPages.currentPage().getParameters().get('contacts'); " This returns a String and you are trying to assing it to List variable...
Trudy DobbelaereTrudy Dobbelaere
Is there a way to pass a list variable into the controller using the getParameter()? I don't see anything that supports this. So even though I already know the results, it sounds like I have to pass the original search parameter (i.e. a string) into the controller and do the query again. That seems like a lot of unnecessary duplicate work.

Trudy, there are many ways to do that, if you are passing in the values in the URL of a page, you need to pass comma seprated values, and in Controller you may use 

List <Contact> results =

This will create you the list, along with it you may use remoting to pass in the value from Page to controller, or you may use Ajax calls..

please mark this answer as best answer if it solves your issue.