I created Order and Order Line Item custom objects they are similar and related to Opportunity and Opportunity Line Item. When I create an Opportunity, a related Order automatically created, when I add a product to Opportunity Line Item, same product be added automatically to Order Line Item of related Order. I don't have a problem with inserting product but I can't make it update. How to achieve it? This is my code for insert but I couldnt make that product on Order Line Item update, please help me. Thanks.
trigger CreateOrderLineItem on OpportunityLineItem (after insert, before delete) {
if(trigger.isInsert){
Map<Id,Product2> proMap = new Map <Id,Product2>([Select Order_line_Items__c From Product2]);
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, IsActive From PricebookEntry Where Id=:opli.PricebookEntryId])
{
for(Pricebook2 pb2 : [Select Id From Pricebook2 Where Id=:pbe.Pricebook2Id])
{
for(Product2 pro : [Select ProductCode, IsActive, 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(product2__c=pro.id, Order__c=orrlist.id, UnitPrice__c=opLi.UnitPrice, Quantity__c = opli.Quantity, ProductCode__c=pro.ProductCode, id=pro.Order_Line_Items__c);
oliList.add(oli);
}
}
}
}
}
}
if(oliList.size()>0)
{
insert oliList;
}
}
Best Answer
I think easiest would be to do a bit more work in after insert, set the Id of the Order Line Item on the Opportunity Line Item. You probably have there something similar to this:
Simply add 1 more field there:
Then in after update you can fetch all matching Opp Line Items:
And go through them
You probably could even improve the last part by querying with "dot" directly (if it will be after update trigger so the new values are saved to database):
No need to use trigger.newMap() anymore, directly overwrite the values
If you don't want to do this extra step during insert (or are worried about existing data) it gets much trickier because how do you know that this line on Opportunity was based on that line on Order? You'd have to somehow consider them matching if for example
Product (or PricebookEntry) + Quantity + Price
match (you could have multiple lines on the order about the same product). But then the whole point of update is that values can change ;) So you would have to be matching OppLineItems in DB with trigger.old but write values from trigger.new. Hardcore...It's doable but a lot can go wrong in code like this. Just identifying the source reference can save you a lot of grief later on. And since it'll be a lookup you could easily build reports that list values side by side to look for problems.
Last edit I'm not touching it anymore, you're doing it wrong and it'll haunt you.