I have a custom object related to Opportunity via not master-detail lookup. I want to delete the custom object record when the Opportunity record is deleted. Thus, I wrote this method into my trigger:

public static void deleteRevPipe (Map<Id, Opportunity> oldMap)
    List<String> oppys = new List<String>();
    List<Revenue_Pipeline__c> revPipe = new List<Revenue_Pipeline__c>();

    for (Opportunity oppy : oldMap.values())
    revPipe = oppys.size() != 0 ? [SELECT Id, Opportunity__c FROM Revenue_Pipeline__c WHERE Opportunity__c IN :oppys] : null;
    delete revPipe;

I've noticed that this only works if it's in beforeDelete context. If I set the context to afterDelete the Revenue_Pipeline record gets updated to clear out the Opportunity lookup field.

I have two related questions.

  1. is it okay to have actions in before context even though it has not been deleted yet?
  2. Should I try to modify the code so that it does work on afterDelete so I'm sure that the record is deleted before I delete the related records?

2 Answers 2


Doing that in a before delete trigger is fine. If something happens to prevent the Opp delete the entire transaction would be rolled back.


Why don't to consider to have a master relationship between those two objects. When the are connected by Master detail you can take advantage of cascade deletion.

  • The record is not always related to the Opportunity. I wanted to but had to move away from that design decision. Mar 2, 2017 at 20:59
  • ok them, if that is the case you need to go on the path of the trigger. Good luck. Mar 2, 2017 at 21:06

You must log in to answer this question.

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