I am trying to add an OpportunityLineItem to an Opportunity and having some challenge with PricebookEntry.
Here is my Apex function:
public static void saveRecord(List recList3){
List<PricebookEntry> pbes = [SELECT Id, PriceBook2Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id IN: recList3 AND PriceBook2.isActive=true LIMIT 1];
for(Integer y = 0, aValue = recList3.size(); y < aValue; y++){
OpportunityLineItem oli = recList3[y];
oli.PriceBookEntryId = pbes.Id;
}
insert recList3;
}
My Apex function receives a List of OpportunityLineItems Object like this:
[{"Name":"ProductXYZ","OpportunityId":"0068A000009sx4UQAQ","Product2Id":"01t6g000000R2L4AAK","Quantity":"1","UnitPrice":"1"}]
What I need is to go through my OpportunityLineItems List, do a SOQL to find the PricebookEntryId that matches the Product2Id and add it to recList3.
I have 2 errors: -Invalid bind expression type of OpportunityLineItem does not match domain of foreign key -Variable does not exist: Id
Thanks for helping me