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
ledap13ledap13 

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);
         }
Best Answer chosen by ledap13
Scott McClungScott McClung
Hi ledap13
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.
public PageReference saveSelectedTariff()
{
  system.debug( '************************************************');
  system.debug( 'Tarifname: '+tariffChosen);

  //Query the PricebookEntry object for all products in the Pricebook that match the tarrifChosen
  List<PriceBookEntry> lstPriceBookEntries = [SELECT Id
                                              FROM PricebookEntry
                                              WHERE Product2Id IN (SELECT Id FROM Product2 WHERE tariff__c = :tariffChosen)
                                              AND Pricebook2Id = myOpportunity.Pricebook2Id];
 
  List<OpportunityLineItem> lstOpportunityLineItems = new List<OpportunityLineItem>(); //needs to be outside the for loop
  for(PriceBookEntry objEntry : lstPriceBookEntries)
  {
    OpportunityLineItem objItemToInsert = new OpportunityLineItem(
                                OpportunityId     = myOpportunity.Id,
                                PricebookEntryId  = objEntry.Id);
    lstOpportunityLineItems.add(objItemToInsert);
   }

  insert lstOpportunityLineItems;
  return null;
}

Hope that helps! :)

All Answers

Anil SavaliyaAnil Savaliya
Hey,

Can You Past whole code ?
ledap13ledap13
Yes of course !!

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;
    }
}
Scott McClungScott McClung
Hi ledap13
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.
public PageReference saveSelectedTariff()
{
  system.debug( '************************************************');
  system.debug( 'Tarifname: '+tariffChosen);

  //Query the PricebookEntry object for all products in the Pricebook that match the tarrifChosen
  List<PriceBookEntry> lstPriceBookEntries = [SELECT Id
                                              FROM PricebookEntry
                                              WHERE Product2Id IN (SELECT Id FROM Product2 WHERE tariff__c = :tariffChosen)
                                              AND Pricebook2Id = myOpportunity.Pricebook2Id];
 
  List<OpportunityLineItem> lstOpportunityLineItems = new List<OpportunityLineItem>(); //needs to be outside the for loop
  for(PriceBookEntry objEntry : lstPriceBookEntries)
  {
    OpportunityLineItem objItemToInsert = new OpportunityLineItem(
                                OpportunityId     = myOpportunity.Id,
                                PricebookEntryId  = objEntry.Id);
    lstOpportunityLineItems.add(objItemToInsert);
   }

  insert lstOpportunityLineItems;
  return null;
}

Hope that helps! :)

This was selected as the best answer