Code 1:
Id oppId = '00612433434'; //some Opportunity Id
for(Opportunity oppObj : [SELECT Id,
(SELECT Product2.Name,Product2.Family FROM OpportunityLineItems)
FROM Opportunity WHERE Id =:oppId]){
System.debug('Hi');
}
code 2:
for (Opportunity opp : [SELECT Id,
(SELECT Product2.Name,Product2.Family FROM OpportunityLineItems)
FROM Opportunity
WHERE Id IN: oppIds]) {
}
Code 1 runs fine in Anonymous window in Developer console but code 2 in the trigger throws error saying
No such relation 'Product2' on entity 'OpportunityLineItem'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
What is wrong I am doing here?