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
ihssan tazinyihssan taziny 

Error on a nested Soql Query

Hi, I'm trying to query from OpportunityLineItem Object using the clause IN with a nested a query: here's the apex code : 
for (PricebookEntry pbe: [Select Product2.Id, Product2.Delivery__c, Product2.Language__c, UnitPrice, Product2.Family from PricebookEntry where Product2Id IN (Select PriceBookEntry.Product2Id from OpportunityLineItem where OpportunityId = : opp.Id) ]){
    // Rest of code
}

But i have this error :
The inner select field 'PriceBookEntry.Product2Id' cannot have more than one level of   relationships

I don't know how to solve this
shiva pendemshiva pendem
Hi ,

I think we cannot have more than one level in inner Query, use the following code to achieve 

//Capture all the product ids 
Set<id> ProductId=new Set<id>();
For(OpportunityLineItem  opline:[Select PricebookEntry.Product2Id from OpportunityLineItem]){
  ProductId.add(opline.PricebookEntry.Product2Id);
}
//Query the related pricebookentries

for (PricebookEntry pbe: [Select Product2.Id, Product2.Delivery__c, Product2.Language__c, UnitPrice, Product2.Family from PricebookEntry where Product2Id IN :ProductId ]){
// Rest of code
}

Hope it will helps you.

Thanks,
Shiva