You need to sign in to do that
Don't have an account?

insert product2Id
Hi friends,
please help me. What is wrong with the code?
listProduct2 = [SELECT id, name, tariff__c from product2 where tariff__c =: tariffChosen];
for (product2 prod : listProduct2) {
List<OpportunityLineItem> listOpportunityLineItem = new List<OpportunityLineItem>();
System.debug('The products : '+prod);
OpportunityLineItem oppliToInsert = new OpportunityLineItem(
OpportunityId = myOpportunity.Id,
Product2Id = prod.id); // ?????????????
listOpportunityLineItem.add(oppliToInsert);
}
please help me. What is wrong with the code?
listProduct2 = [SELECT id, name, tariff__c from product2 where tariff__c =: tariffChosen];
for (product2 prod : listProduct2) {
List<OpportunityLineItem> listOpportunityLineItem = new List<OpportunityLineItem>();
System.debug('The products : '+prod);
OpportunityLineItem oppliToInsert = new OpportunityLineItem(
OpportunityId = myOpportunity.Id,
Product2Id = prod.id); // ?????????????
listOpportunityLineItem.add(oppliToInsert);
}
You're running into an issue because OpportunityLineItem does not have a direct relationship with the Product2 object so there isn't a Product2Id field to populate.
PriceBookEntry is the junction object between OpportunityLineItem and Product2 so you want to populate the PricebookEntryId field on OpportunityLineItem instead.
Here's one way you could adjust your code to accomplish it.
Hope that helps! :)
All Answers
Can You Past whole code ?
public PageReference saveSelectedTariff() {
system.debug( '************************************************');
system.debug( 'Tarifname: '+tariffChosen);
listProduct2 = [SELECT id, name, tariff__c from product2 where tariff__c =: tariffChosen];
for (product2 prod : listProduct2) {
List<OpportunityLineItem> listOpportunityLineItem = new List<OpportunityLineItem>();
System.debug('The products : '+prod);
OpportunityLineItem oppliToInsert = new OpportunityLineItem(
OpportunityId = myOpportunity.Id,
Product2Id = prod.id);
listOpportunityLineItem.add(oppliToInsert);
}
insert listOpportunityLineItem;
return null;
}
}
You're running into an issue because OpportunityLineItem does not have a direct relationship with the Product2 object so there isn't a Product2Id field to populate.
PriceBookEntry is the junction object between OpportunityLineItem and Product2 so you want to populate the PricebookEntryId field on OpportunityLineItem instead.
Here's one way you could adjust your code to accomplish it.
Hope that helps! :)