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
Luis MurilloLuis Murillo 

Error list apex with a SoQL script using multiple sObjects

Hi Salesforce Community,

I got an error list apex with a SoQL script using multiple sObjects: Opportunity & OpportunityLineItems.


SELECT Amount, Name, ( SELECT Quantity, ListPrice,PriceBookEntry.ProductCode, PriceBookEntry.UnitPrice,TotalPrice, PricebookEntry.Name,PricebookEntry.product2.Family FROM OpportunityLineItems ) FROM Opportunity where id ='006i00000026VOj'

It is all the data that I need to show into a table visualforce page. But the list apex class, has an error. : Illegal assignment from LIST<Opportunity> to LIST<OpportunityLineItem>

I think this happens because maybe I need 2 lists to each sObjects. Actually I am using just 1 list with all my script SoQL.



public class clsPaginaPedidos {

    public Opportunity opp {get;set;}    

   List<OpportunityLineItem> OpportunityLineItem;  //List        


public List<OpportunityLineItem> getOpportunityLineItem() { 

 if(OpportunityLineItem == null) OpportunityLineItem = [SELECT Amount, Name,

(SELECT Quantity, ListPrice,PriceBookEntry.ProductCode, PriceBookEntry.UnitPrice,TotalPrice, PricebookEntry.Name, PricebookEntry.product2.Family FROM OpportunityLineItems)

FROM Opportunity where id = :ApexPages.currentPage().getParameters().get('id')];

return OpportunityLineItem;         

    }//End List  


}//End class


Any help??




The reason is because the SOQL is returning Opportunity records, and you are trying to store it into a list of OpportunityLineItem.

List<Opportunity> Opportunities;



yes, as mentioned above correctly - you're trying to assign Opportunity records to a List of OpportunityLineItems.


SELECT Amount, Name, ( SELECT Quantity, ListPrice,PriceBookEntry.ProductCode, PriceBookEntry.UnitPrice,TotalPrice, PricebookEntry.Name,PricebookEntry.product2.Family FROM OpportunityLineItems ) FROM Opportunity where id ='006i00000026VOj'


In the above query, the inner SOQL returns the child records from the main (Parent object). So here you'll be returned a List of Opportunity records with LineItem records for each of the Opportunity.


One point to be noted - You have hard-coded the Opportunity Id. Not sure if it is only for testing purpose. But make sure you never do that.