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
Austin GutzAustin Gutz 

Query for Opportunity Product Line items

I have found some posts about how to query for line item values but I can't seem to find out how to format the query based on line item values. 

Example:

SELECT Name, (Select PricebookEntry.Product2Id,Certainty_Percentage__c, TotalPrice, From OpportunityLineItems) 

From Opportunity

WHERE 
 
Certainty_Percentage__c = '90%'   

The problem is I want the certain percentage value on the line item, not the opportunity itself. I tried just doing "From OpportunityLineItems" but I get an error every time. 
HARSHIL U PARIKHHARSHIL U PARIKH
Can you share the error though..?

Something like these won't work?
 
List<Opportunity> oppsWithItems = New List<Opportunity>();

oppsWithItems = [SELECT Name, (Select PricebookEntry.Product2Id,Certainty_Percentage__c, TotalPrice, From OpportunityLineItems WHERE Certainty_Percentage__c LIKE :('%' + 90 + '%')) From Opportunity];

Hope it helps!
Austin GutzAustin Gutz
Well... this is awkward... It looks like all I need to do is use 
FROM OpportunityLineItem

NOT

FROM OpportunityLineItemS

The error I was getting was:

 "FROM OpportunityLineItems 
     ^
ERROR at Row:3:Column:6
sObject type 'OpportunityLineItems' is not supported."
HARSHIL U PARIKHHARSHIL U PARIKH
If the OpportunityLineItem query is inside of an opportunity query then you need to use S

This would throw an error:
List<Opportunity> oppWithItems = [Select Id, Name,
                                               (Select PricebookEntry.Product2Id FROM OpportunityLineItem) FROM Opportunity];
This would not:
List<Opportunity> oppWithItems = [Select Id, Name,
                                               (Select PricebookEntry.Product2Id FROM OpportunityLineItems) FROM Opportunity];