You need to sign in to do that
Don't have an account?
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.
Script:
-------------------------
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.
Apex:
-----------------
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??
Thanks,
Luis.
The reason is because the SOQL is returning Opportunity records, and you are trying to store it into a list of OpportunityLineItem.
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.