Trigger that saves all the activities and events of a contact before a contact is deleted. When a contact is deleted all the history related to that contact is deleted.

  • 4
    Welcome to SFSE. What did you try so far? Are you stuck with something?
    – metasync
    Commented Apr 11, 2018 at 16:36
  • I have created a trigger that is invoked before deletion. I am retrieving all the events data from the contact. Now I'm stuck at creating a dummy contact and linking this event data to that contact.
    – New
    Commented Apr 11, 2018 at 16:58
  • You should edit your question to include your trigger code. Commented Apr 11, 2018 at 17:26
  • i have included my code.
    – New
    Commented Apr 11, 2018 at 17:36
  • What you're doing doesn't make much sense looking at that code snippet, you should draw this out
    – EricSSH
    Commented Apr 11, 2018 at 17:42

1 Answer 1


Your trigger has a number of logical errors.

  1. You are querying against the wrong Contact. You query against the WhoId of your dummy contact, while what you probably want to be doing is querying against a collection of deleted Contact Ids like oldMap.keySet() outside your for loop. These are the records whose Events or Tasks you wish to preserve.
  2. You are not making any changes to the events, like reassigning their WhoId. (If that is in fact what you plan to do).
  3. You are attempting to insert existing Event records, with a populated Id field, using Database.insert(). This will fail; you cannot insert records with an Id, and it looks like you probably meant to perform an update anyway.
  4. You've named a variable event. Since Apex is a case-insensitive language, this looks the same as the class name Event to the compiler, which can cause very confusing errors. You do the same thing with contact.
  5. You initialize events with an empty list, which you don't need to do since you populate it from a query.
  6. You do not need to check Trigger.isDelete && Trigger.isBefore, since your trigger runs only on the before delete event.

I would suggest that you take some time to write out, in pseudo-code, what the logical flow of your trigger should look like, before you begin translating it into Apex. If you haven't already worked through Trailhead's trigger modules, especially Bulk Triggers, they might be a great help to you.

Not the answer you're looking for? Browse other questions tagged .