function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
karol.freebergkarol.freeberg 

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
karol.freebergkarol.freeberg
Thank you. I thought that was the case.