You need to sign in to do that
Don't have an account?
Benny Stevens 1
Trigger OLI creates duplicate record
Hi,
I think I have this simple trigger on an Opportunity Product with a Status of Approved, to create a record in a custom object FPS, but when it meets the criteria, it creates 2 records.
Strange thing is that after that, when the Opportunity Product is updated again and it still meets the criteria, that it then creates only 1 record. Also when I have removed the first 2 records that have been created ... It is only the first time the trigger fires for that Opportunity Product, that it will create 2 records.
Thanks,
Benny
I think I have this simple trigger on an Opportunity Product with a Status of Approved, to create a record in a custom object FPS, but when it meets the criteria, it creates 2 records.
Strange thing is that after that, when the Opportunity Product is updated again and it still meets the criteria, that it then creates only 1 record. Also when I have removed the first 2 records that have been created ... It is only the first time the trigger fires for that Opportunity Product, that it will create 2 records.
Thanks,
Benny
trigger CreateFPS on OpportunityLineItem (after update) { for (OpportunityLineItem u : Trigger.new) { if (u.Status__c == 'Approved') { FPS__c x = new FPS__c(); x.Opportunity__c = u.OpportunityId; x.Sales_Price__c = u.UnitPrice; x.Phase__c = 'Nomination'; insert x; } } }
http://developer.force.com/cookbook/recipe/controlling-recursive-triggers
All Answers
The debug log should help in this situation - it will show you all the triggers/workflow etc that are being evaluated for the OLI.
Out of curiosity - do you want to create another record each time the OLI is updated - I would have thought you just wanted it when the OLI transitions to the 'Approved' status, rather than every time it is edited and the status equals 'Approved'.
Also, you shouldn't have a DML statement in a for loop.. With your code above, if you had 200 new OpportunityLineItem inserted in one go (e.g. via dataloader) you will very quickly hit governor limits.
You can improve the code like this:
No, I only want to create a record the first time it reaches that Approved Status.
I just started with APEX since a few weeks and it was going to be the next thing to find out :-)
Thanks Shyam, great help!
I don't have any triggers so far on the FPS object.
You are correct, I have 4 WFR's on OLI that are updating fields when the status is equal to Approved and some other criteria.
I have deactivated them and then it only create 1 record for the custom object.
I still need those WFR's. How can I avoid that the trigger creates multiple records, while I still have the WFR active?
I am looking into "if(trigger.isupdate) and oldmap" to avoid that it triggers everytime it is updated when the status is Approved.
Thanks,
Benny
I need to have the WFR with field updates active. How can I avoid that a duplicate record is update because of the active WFR's?
Thanks, Benny
http://developer.force.com/cookbook/recipe/controlling-recursive-triggers
One more hurdle to take - I hope - to get the trigger working correctly ...
There is a workflow rule that updates the unit price of the OLI, but the record for the custom object FPS is added before the workflow rule is updating the OLI.
So the FPS record is taking the old unit price, while it should take over the WFR updated unit price!
I have chosen AFTER UPDATE, but this doesn't prevent that the record is added before the WFR is updating the Sales Price field.
Trigger
Class