[SalesForce] Trigger to add Products to Opportunity Line Item from a custom object without Price Book

I have custom objects named Order and Order Line Item, just like Opportunity and Opportunity Line Item, they have same fields and relationships but Order Line Item has no need for Pricebook, products can be added directly. Also Order object has Lookup to Opportunity so every Order has a related Opportunity. What I need is when add a product to Order Line Item of the Order, same product must be added to Opportunity Line Item of the related Opportunity.

I have this trigger but i get some errors:

trigger CreateOpportunityProduct on Order_Line_Item__c(after insert, after update){       
if(trigger.isInsert)
{          
List<ID> pricebookID=new List<ID>();       
List<ID> opportunityID=new List<ID>();           
List<OpportunityLineItem> newopportunitylineitemlist=new List< OpportunityLineItem>();        
for(Order_Line_Item__c orderlist: Trigger.new){    
OpportunityLineItem Oppitem=new OpportunityLineItem();
Oppitem.Quantity=orderlist.Quantity__c;
Oppitem.UnitPrice=orderlist.UnitPrice__c;
pricebookID.add(Oppitem.PriceBookEntryId);
opportunityID.add(Oppitem.OpportunityId);
newopportunitylineitemlist.add(Oppitem);
}    
List<Order__c> orderlist= new List<Order__c>([Select Id,Opportunities__c From Order__c Where Opportunities__c IN:opportunityID]);            
List<PricebookEntry> pricebookentrylist = new List<PricebookEntry>([Select Id,Product2Id From PricebookEntry Where Id IN:pricebookID]);         
if(newopportunitylineitemlist.size()>0)
insert(newopportunitylineitemlist);    
}    
}

And the error is:REQUIRED_FIELD_MISSING, Required fields are missing: [Opportunity]: [Opportunity]: Trigger.CreateOpportunityProduct: line 20, column 1

Best Answer

As the error states, you need to specify the opportunity id. Presumably, you'll find the opportunity attached to the order.

That probably looks like this:

trigger CreateOpportunityProduct on Order_Line_Item__c(after insert, after update){       
if(trigger.isUpdate)
{          
    map<id, order__c> orders = new map<id, order__c>();
List<ID> pricebookID=new List<ID>();       
List<ID> opportunityID=new List<ID>();           
List<OpportunityLineItem> newopportunitylineitemlist=new List< OpportunityLineItem>();        
    for(order_line_item__c record: trigger.new) {
        orders.put(record.order__c, null);
    }
    orders.putall([select id, opportunity__c from order__c where id in :orders.keyset()]);
for(Order_Line_Item__c orderlist: Trigger.new){    
OpportunityLineItem Oppitem=new OpportunityLineItem();
Oppitem.Quantity=orderlist.Quantity__c;
Oppitem.UnitPrice=orderlist.UnitPrice__c;
oppitem.opportunityid = orders.get(orderlist.order__c).opportunity__c;
pricebookID.add(Oppitem.PriceBookEntryId);
opportunityID.add(Oppitem.OpportunityId);
newopportunitylineitemlist.add(Oppitem);
}    
List<Order__c> orderlist= new List<Order__c>([Select Id,Opportunities__c From Order__c Where Opportunities__c IN:opportunityID]);            
List<PricebookEntry> pricebookentrylist = new List<PricebookEntry>([Select Id,Product2Id From PricebookEntry Where Id IN:pricebookID]);         
if(newopportunitylineitemlist.size()>0)
insert(newopportunitylineitemlist);    
}    
}

Adjust field names as necessary.