+ Start a Discussion

Delete Trigger with a Master Lookup relationship question.

I have a custom object (Portal_User__C) with a field called user_name__c. The user_name__c field has a master lookup relationship to the contact record. I have a before delete trigger on the Portal_User__c record that sends an email to the portal administrator. It works fine if I just delete the portal_user__c record. However; if I delete the contact record first, by design it does delete the portal_user__c record but the trigger does not work. Do I have to write two triggers?  (One for the contact record to check for portal_user__c records before it can be deleted. And a second one for the actual Portal_user__c.) Or how does that work?
James LoghryJames Loghry
Natively, detail records are cascade-deleted if their master records are deleted. However, when a detail record is deleted in this fashion, then the detail record triggers do NOT fire.  To get around this, you'll need separate delete triggers, one for the detail record, and one for the master record.  Your master record would either just delete the children, or it would send the emails on behalf of the children records.  Please note that you may have several children records, and will need to handle those situations gracefully.

Here's another thread on a related topic on stack exchange: http://salesforce.stackexchange.com/questions/23198/delete-triggers-dont-cascade-master-to-detail-so-why-not-hand-cascade-on-the-m
Thank you. I thought that was the case.