You need to sign in to do that
Don't have an account?
Patricia Mozzoni 9
First trigger & need help correcting error
Thanks for taking the time to read this! This is my first trigger and the purpose is to update the UnitPrice field on Opportunity Line Items with the value of a formula field Net_Amount__c. I don't even understand the error message I received and will share below.
Greatly appreciate any help!
My Trigger:
Greatly appreciate any help!
My Trigger:
trigger oliUpdate on OpportunityLineItem (after insert, after update) { Set <String> oliID = New Set <String> (); For (OpportunityLineItem oli: Trigger.new) { if (oli.OpportunityId != Null ) { oliID.add (oli.Id); } } If (oliID.size ()> 0) { List <OpportunityLineItem> upOpiList = new List <OpportunityLineItem> (); For (OpportunityLineItem ol: [SELECT Id, Net_Amount__c, UnitPrice FROM OpportunityLineItem WHERE id in: oliID AND Net_Amount__c > 0.0]) { ol.UnitPrice = ol.Net_Amount__c; UpOpiList.add (ol); } If (upOpiList.size ()> 0) update upOpiList; } }
Please use below code:-
If this solution is usefull for you, Please mark as a Best Answer to help others.
Regards
Mukesh
All Answers
oliUpdate: maximum trigger depth exceeded OpportunityLineItem trigger event AfterInsert OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate OpportunityLineItem trigger event AfterUpdate
The error you're facing is because of your trigger going into recursion.
Why is this happening?
Your trigger is updating Opportunity Line Items, which then fires the same trigger again and again, so recursion.
What should you do to fix this?
There are many blogs and articles available on what should you do to fix this, sharing one for example
https://www.sfdcpoint.com/salesforce/maximum-trigger-depth-exceeded-error-salesforce/#:~:text=Maximum%20Trigger%20Depth%20Exceeded%20Error%20Salesforce%20occurs%20mainly%20due%20to,result%20in%20governor%20limit%20sometime.
For your current use case, you don't even need an after insert / update trigger, because you are updating the same record.
This should be done with an before insert / update trigger.
Even better if you use Process builder or flow as your use case is straightforward where you're copying value from one field to the other.
I'm sharing a modified trigger that you can use for your use case.
Hope this helps.
Best,
Vishal
Please use below code:-
If this solution is usefull for you, Please mark as a Best Answer to help others.
Regards
Mukesh
Anyway, the trigger works in the sandbox and now I am attempting test code. Can either of you review and see what I am doing wrong? It seems to be harder then the trigger itself! This is what I have so far. As you can likely tell, I have been looking at many examples to determine how to write the test case, but now have become confused. Please point in the right direction.
These are the errors I am getting: