I created 2 custom objects Order and Order Line Item, they are same with Opportunity and Opportunity Line Item. Also Order and Opportunity has lookup, so every Opportunity has a related Order object. What I want is when I insert a product to Opportunity Line Item, same product to custom Order Line Item of related Order automatically inserted.
I have this trigger, it gives no error but nothing happens when I run, what am I missing?:
trigger CreateOrderLineItem on OpportunityLineItem (after insert, before update) {
List<Order_Line_Item__c> oliList = new List<Order_Line_Item__c>();
for(OpportunityLineItem opli : [Select opportunityid, Id, pricebookentryid, unitprice, quantity From OpportunityLineItem Where id IN:Trigger.newMap.keyset()])
{
for(Opportunity oppList : [Select id, Orders__c From Opportunity Where id=:opli.OpportunityId])
{
for(PricebookEntry pbe : [Select pricebook2id, product2id, unitprice, name From PricebookEntry Where Id=:opli.PricebookEntryId])
{
for(Pricebook2 pb2 : [Select Id From Pricebook2 Where Id=:pbe.Pricebook2Id])
{
for(Product2 pro : [Select id, Name, ProductCode, Order_Line_Items__c From Product2 Where Id =: pbe.product2id])
{
for(Order__c orrList : [Select Id, Opportunities__c From Order__c Where Opportunities__c =:oppList.id])
{
Order_Line_Item__c oli = new Order_Line_Item__c(Name='Opp-Product', Product2__c=pro.Id, UnitPrice__c=opli.UnitPrice, Quantity__c = opli.Quantity, Product_Name__c=pro.Name, Order__c=oppList.Orders__c, id = pro.Order_Line_Items__c);
oliList.add(oli);
}
}
}
}
}
if(oliList.size()>0){
insert oliList;
update oliList;
}
}
}
a handful
.