+ Start a Discussion
Priyanka7Priyanka7 

how to insert a product in opportunitylineitem

Hi Guys,

I have a custom object that has lookup relationship with Product,When i select a Product its related record should be inserted in OpportunityLineItem.Please help me out!!!!

Thanks in advance.

prateek jainprateek jain
Hi
Can you share the structure of your object ,do you have opportunity id 
Give more details on what do you want to achieve and structure of you object
Thanks
Priyanka7Priyanka7
Hi Prateek,
Here is my code, how to insert further? Please help me!!

trigger showRelatedList on OpportunityLineItem (after insert,after update) {   
    List<OpportunityLineItem> ol = new List<OpportunityLineItem>();
    
    Product_Group__c p = [Select Master_Product__c from Product_Group__c];
    List<Product_Group__c> pg = [Select id, Child_Product__c from Product_Group__c where Master_Product__c=:p.Id];
    PriceBookEntry price = [Select Product2Id from PriceBookEntry ];
    List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry where Product2Id=:price.Product2Id];
     //List<OpportunityLineItem> li = [select id, PriceBookEntryId from OpportunityLineItem];
    PriceBookEntry pbe = [Select id from PriceBookEntry];
    for(OpportunityLineItem oli : Trigger.new){
           
        oli.PricebookEntryId = pbe.Id;
        oli.Product2Id = pbe.Product2Id;
        ol.add(oli);
   }  
  
insert ol;

}
prateek jainprateek jain
Hi 
In this line of code 
Product_Group__c p = [Select Master_Product__c from Product_Group__c]; 
what is master __Product__c
In my view 
it should be 
list<Product_Group__c> p= [Select Master_Product__c from Product_Group__c]; 
or there must be where clause in the query
then you must iterate through the list.
Please  share  exact  problem you are facing, It will be really helpful 
I will be happy to help you out.
Thanks 
Priyanka7Priyanka7

Hi,

I have done some changes but got the following error "Apex trigger showRelatedList caused an unexpected exception, contact your administrator: showRelatedList: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 1; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]: Trigger.showRelatedList: line 42, column 1  "


My code:

trigger showRelatedList on OpportunityLineItem (after insert,after update) {  
    
    String oliId ;
   
    String opp;
    String pricebook;
     
     Set<ID> ids = new Set<ID>();
     List<Product_Group__c> prog = New List<Product_Group__c>();
    
                for(OpportunityLineItem li : Trigger.new){
                        opp = li.OpportunityId;
                        oliId = li.Product2Id;
                             
                }
         System.debug('@@@'+opp);
        System.debug('<<>>'+oliId);
               
               prog = [Select id, child_product__c from product_group__c where Master_Product__c=:oliId ];
                    for (Product_Group__c pr:prog){
                        ids.add(pr.child_product__c);
        
                    }
  System.debug('$$$$'+ids);

    List<OpportunityLineItem> ol = new List<OpportunityLineItem>();  
   
    List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id,Pricebook2Id,UnitPrice FROM PriceBookEntry where Product2Id In :ids  ];
    
            
        for(PriceBookEntry  pg : priceBookList){
          OpportunityLineItem o = new OpportunityLineItem(OpportunityId=opp, PricebookEntryId=pg.Id, Quantity=1,TotalPrice=pg.UnitPrice );
        ol.add(o);
            
        }
        System.debug('//////'+ol);
    
 
    
  
insert ol;

}