1

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?

1 Answer 1

1

Product2Id was added as a read-only lookup field to OpportunityLineItem in API v30.0

If your trigger is using a version of the API prior to v30.0, you'll need to go through PricebookEntry to get to Product2 on an OpportunityLineItem, or update the trigger's metadata to use API v30.0 or newer.

If you don't want to (or otherwise can't) update the API version used by your trigger, change your query to be

[SELECT Id, 
    (SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems)
FROM Opportunity
WHERE Id IN: oppIds
]
3
  • Code 1 runs fine in developer console in anonymous window. Why is that??
    – javanoob
    Oct 6, 2015 at 18:34
  • After running your code snippet myself, I'm not sure. I thought that OpportunityLineItem only had a standard lookup to PricebookEntry, but after taking another look, there is indeed a standard lookup to Product2 as well (in my Summer '15 production org). Perhaps this was introduced in an API version which is newer than the API version your trigger uses.
    – Derek F
    Oct 6, 2015 at 18:58
  • My hunch was right, the Product2Id lookup field was added to OpportunityLineItem in API v30. Updating my answer to reflect this.
    – Derek F
    Oct 6, 2015 at 19:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .