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
Douglas MolinaDouglas Molina 

Create custon Visualforce page list, from Apex SOQL query with filds from multiple Objects

Hi,

I have in my Org the WorkOrder and WorkOrderLineItens Objects.
I want to display the result of a very specific SOQL query in a Visualforce page.
The point is, in my Apex SOQL query, I bring fields from both objects, and others related... so, how can I declare a single List to receive this SOQL result, with all the returned fields, from all objects?

I'm following this article, but It only uses fields fron a single object, so the List is declared based on that object..
https://help.salesforce.com/articleView?id=000205631&type=1

My SOQL looks like this:
select  	id, LineItemNumber, 
			PricebookEntry.Product2.Name,
			WorkOrder.WorkOrderNumber, WorkOrder.Tipo_de_Servi_o__c
FROM		WorkOrderLineItem
WHERE		UnitPrice = 0	AND 	
			WorkOrder.CreatedDate >= 2017-05-15T00:00:00.000+0000
ORDER BY	LineItemNumber ASC

In developer console, it runs fine:
User-added image

Some sugestion, or other reference that could help me on it?
Thanks.
Best Answer chosen by Douglas Molina
Sukanya BanekarSukanya Banekar
Hi,
Yes you can put the query result in list. and access that on visulforce page. For example 
Put the query in lstObjects list. Make the list as property

Apex class
list<WorkOrderLineItem> lstObjects {get;set;}
lstObjects = new list<WorkOrderLineItem> ();
lstObjects = [select      id, LineItemNumber, 
                                    PricebookEntry.Product2.Name,
                                    WorkOrder.WorkOrderNumber, WorkOrder.Tipo_de_Servi_o__c
                      FROM    WorkOrderLineItem
                      WHERE  UnitPrice = 0    AND     
                                     WorkOrder.CreatedDate >= 2017-05-15T00:00:00.000+0000
                 ORDER BY  LineItemNumber ASC];

VF page:
<apex:page controller="classController">
    <apex:repeat value="{!lstObjects}" var="lst">
        <apex:outputText value="{!lst.WorkOrder.WorkOrderNumber}">
    </apex:repeat>
</apex:page>

Hope this will help you.

Thanks,
Sukanya Banekar

All Answers

Amit Singh 1Amit Singh 1
Yes, you can put all the related Object into Single List if all the fields are being retrieved using the same SOQL for example If you want List for the Above Query then use List<WorkOrderLineItem> 

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_using.htm#sforce_api_calls_soql_relationships_query_using (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_using.htm#sforce_api_calls_soql_relationships_query_using" target="_blank)
Sukanya BanekarSukanya Banekar
Hi,
Yes you can put the query result in list. and access that on visulforce page. For example 
Put the query in lstObjects list. Make the list as property

Apex class
list<WorkOrderLineItem> lstObjects {get;set;}
lstObjects = new list<WorkOrderLineItem> ();
lstObjects = [select      id, LineItemNumber, 
                                    PricebookEntry.Product2.Name,
                                    WorkOrder.WorkOrderNumber, WorkOrder.Tipo_de_Servi_o__c
                      FROM    WorkOrderLineItem
                      WHERE  UnitPrice = 0    AND     
                                     WorkOrder.CreatedDate >= 2017-05-15T00:00:00.000+0000
                 ORDER BY  LineItemNumber ASC];

VF page:
<apex:page controller="classController">
    <apex:repeat value="{!lstObjects}" var="lst">
        <apex:outputText value="{!lst.WorkOrder.WorkOrderNumber}">
    </apex:repeat>
</apex:page>

Hope this will help you.

Thanks,
Sukanya Banekar
This was selected as the best answer
Mustafa JhabuawalaMustafa Jhabuawala
Basically you need to prepare a list of object which your SOQL query is targetting.

So if your SOQL query is having FROM WorkOrderLineItem then you should prepare the list of object type WorkOrderLineItem and the result will be stored in it.
Douglas MolinaDouglas Molina
Thank you Amit Singh 1, Sukanya Banekar, and Mustafa Jhabuawala.

Your sugestions worked fine, and helped me to understand these lists.
I thought the list of objects could not hold related objects fields, cause I was reciving some "invalid field" error messages on saving the visualforce page.
The problem is that I was referring to a related object field, just writing its name after the var name .. without the relationship field name between them.
Now it's working as I spected.

Follow is the reduced version of the Apex Class:
public with sharing class ListaItensZerados {
    public list<WorkOrderLineItem> lista {get;set;}
    
    public ListaItensZerados(){
        lista = new list<WorkOrderLineItem>();
        lista = [
            select      id, LineItemNumber,
                        PricebookEntry.Product2.Name,
                        WorkOrder.WorkOrderNumber, WorkOrder.Tipo_de_Servi_o__c,
                        WorkOrder.T_cnico_Respons_vel__c
            FROM        WorkOrderLineItem
            WHERE        UnitPrice = 0    AND     
                        WorkOrder.CreatedDate >= 2017-05-15T00:00:00.000+0000
            ORDER BY    LineItemNumber ASC            
        ];  
    }
}

And the visualforce page:
<apex:page controller="ListaItensZerados"> 
	<apex:pageBlock title="My Content"> 
		<apex:pageBlockTable value="{!lista}" var="registro"> 
			<apex:column > 
				<apex:facet name="header">Responsável:</apex:facet> 
				<apex:outputText value="{!registro.WorkOrder.T_cnico_Respons_vel__c}"/> 
			</apex:column> 
			<apex:column > 
				<apex:facet name="header">Número da OT:</apex:facet> 
				<apex:outputText value="{!registro.WorkOrder.WorkOrderNumber}"/> 
			</apex:column> 
            <apex:column > 
				<apex:facet name="header">Tipo de Serviço:</apex:facet> 
				<apex:outputText value="{!registro.WorkOrder.Tipo_de_Servi_o__c}"/> 
			</apex:column> 
            <apex:column > 
				<apex:facet name="header">Número do Item</apex:facet> 
				<apex:outputField value="{!registro.LineItemNumber}"/> 
			</apex:column> 
            <apex:column > 
				<apex:facet name="header"> Nome do Item</apex:facet> 
				<apex:outputText value="{!registro.PricebookEntry.Product2.Name}"/> 
			</apex:column>
		</apex:pageBlockTable> 
	</apex:pageBlock> 
</apex:page>

The result page:
User-added image

Thank you again!