+ Start a Discussion
ShadowlessKickShadowlessKick 

Contacts associated to Deleted Account

When an Account is deleted the related Contacts are also removed from Salesforce.  What event eliminates them?  I have created a trigger to capture the ID of the contacts when they are deleted. It is successful when the contacts are deleted individually or merged through the UI.  However, when they are removed with an Account deletion the ID's are not captured.  The "After Delete" trigger on the contact does not seem to fire.

Is there some other kind of event that needs to be monitored?
Best Answer chosen by ShadowlessKick
GlynAGlynA
Contacts are deleted because their lookup relationship with the Account is a "cascade delete" relationship.  Cascade deletes don't run triggers.  This is also true when Detail records are deleted because their Master record is deleted.

The best you can do is to trigger "before delete" on Account and query the related Contacts to get their IDs before they get deleted.

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

All Answers

GlynAGlynA
Contacts are deleted because their lookup relationship with the Account is a "cascade delete" relationship.  Cascade deletes don't run triggers.  This is also true when Detail records are deleted because their Master record is deleted.

The best you can do is to trigger "before delete" on Account and query the related Contacts to get their IDs before they get deleted.

Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy@blogspot.com
Twitter: @GlynAtClosedWon
This was selected as the best answer
ShadowlessKickShadowlessKick
Thank you for your advice.  The trigger ended up looking like this.

trigger DeletedAccountContactHistory on Account (before delete) {
List<Id>theIDs = new List<id>();
EntityHistory__c deletedRecord = new EntityHistory__c();
List <EntityHistory__c> listDeletedRecords = new List <EntityHistory__c>();

      for (Account theAccount:trigger.old)
      {
       //Build List of IDs
       theIDs.add(theAccount.id);  //add account ID for each record deleted
      }

       List<Contact> deletedContacts=[select id, AccountId,  Name from contact where AccountId IN: theIDs];
       for (Contact theContact: deletedContacts)
       {
        deletedRecord = new EntityHistory__c(); 
        deleted.Name = 'Contact: ' + theContact.Name;
        deletedRecord.Deleted_Contact_ID__c = theContact.id;
        deletedRecord.Deleted_Account_ID__c = theContact.AccountId;
        deletedRecord.Type__c = 'Cascade';
        listDeletedRecords.add(deletedRecord);
       }
       //Insert all the records into the Entity History file that were processes with this run  
       if (listDeletedRecords.isEmpty() == false){
      try
        {
            insert listDeletedRecords;
        }
     catch(Exception ex)
     {   
        System.debug('\n\nException ='+ex.getMessage()+'\n\n');
     }
  
}//End If
      
GlynAGlynA
Looks good!  You probably already found this, but I think the line:

deleted.Name = 'Contact: ' + theContact.Name;

should be:

deletedRecord.Name = 'Contact: ' + theContact.Name;

-Glyn
GlynAGlynA
You could also write:

<pre>
listDeletedRecords.add
(   new EntityHistory__c
    (   Name = 'Contact: ' + theContact.Name,
        Deleted_Contact_ID__c = theContact.id,
        Deleted_Account_ID__c = theContact.AccountId,
        Type__c = 'Cascade'
    )
);
</pre>

Then you won't need "deletedRecord" at all.

-Glyn