You need to sign in to do that
Don't have an account?
ShadowlessKick
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?
Is there some other kind of event that needs to be monitored?
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
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
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
}
deleted.Name = 'Contact: ' + theContact.Name;
should be:
deletedRecord.Name = 'Contact: ' + theContact.Name;
-Glyn
<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