+ Start a Discussion
Jeff TalbotJeff Talbot 

line item trigger not firing with opportunity delete

I have a custom object "Inventory Movement". On Opportunity Line Items, I have a custom lookup field that links the Line Item record to an "Inventory Movement" record. If the Line Item is deleted, the Inventory Movement record needs to be deleted.

I have successfully implemented an "after delete" Trigger on Opportunity Line Items. When I delete an Opportunity Line Item, the Trigger deletes the Inventory Movement record.

I'm not clear why my trigger doesn't work when I delete an Opportunity. When an Opportunity is deleted, its line items are automatically deleted by Salesforce system functionality. How come this auto deletion of line items doesn't fire the Trigger?
Best Answer chosen by Jeff Talbot
DML performed as a result of some automation functions, such as cascade deletion in this case, do not fire triggers.  The solution is to move your Inventory Movement logic into a class that can be invoked by both the Opportunity Line Item trigger and the Opportunity trigger.  When an Opportunity is deleted, query its Opportunity Line Items and pass those to the Inventory Movement logic as if they had been deleted already (which they will be soon enough).

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Twitter: @GlynAtClosedWon