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 cannot give Lookup between Order Line Item and Opportunity Line Item, so don't know how get Opportunity, PriceBookEntryId values. I tried it in this trigger:
trigger CreateOpportunityProduct on Order_Line_Item__c(after insert, after update){
List<OpportunityLineItem> OppLineList = new List<OpportunityLineItem>();
for(Order_Line_Item__c OrderLineObj : Trigger.new){
OpportunityLineItem OppL = new OpportunityLineItem(
Opportunity=OrderLineObj.?, //Opportunity field is required but I don't know how to get it.
PricebookEntryId=OrderLineObj.Price_Book__c, //I done a lookup named Price_Book__c to Price Book object but I don't know if it's helps.
UnitPrice=OrderLineObj.UnitPrice__c,
Quantity=OrderLineObj.Quantity__c
);
OppLineList.add(OppL);
}
if(OppLineList.size()>0)
insert(OppLineList);
}
I write this trigger now but still having some errors, how can I make it run:
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); //Error: Compile Error: Invalid field PriceBookEntryId for SObject Order_Line_Item__c at line 11 column 17
opportunityID.add(Oppitem.OpportunityId); //Error: Compile Error: Invalid field OpportunityId for SObject Order_Line_Item__c at line 12 column 19
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);}
}
na1
,eu1
orlogin
would work equally well...