+ Start a Discussion
srishsrish 

Trigger to delete master record when child is deleted

It is a salesforce standard functionality that when two objects are in a Master-detail relatonship with each other then if the Master record is deleted, all the related child records get delted automatically. However, what if we have a vice-versa situation? i.e. when a child record is deleted then the Master record without any child should also get deleted. This can be achieved by a trigger!! :)

Below is the code for the same. (Note: this code does not work for deleting records in bulk. Will be soon posting that as well. Also we need to have a unique key on both the objects)

if(Trigger.isAfter){
            if(Trigger.isDelete){
 
                List<ChildObjName__c> IdSetC = new List<ChildObjName__c>(); 
                List<MasterObjName__c  IdSetM = new List<MasterObjName__c>();
                Set<Id> IdSet = new Set<Id>();
               
                for(ChildObjName__c Cobj : Trigger.Old){
                     IdSet.add(Cobj.RelationshipName__c);
                     }
                List<MasterObjName__c> MobjLst = new List<MasterObjName__c>( [select id from MasterObjName__c where id =:IdSet]);
   
                    IdSetM=[select KeyField__c from MasterObjName__c where id=:IdSet];
                    IdSetC = [select id  from ChildObjName__c where KeyField__c =:IdSetM[0].KeyField__c];
   
                    if (IdSetC.size()==0)
                        {
                            delete MobjLst;
                        }
                    }
                } 

Solution by Abinash Sahoo and Srishti Goyal
Best Answer chosen by srish
srishsrish
Thank you Balaji Bondar for the code, it rellay helped me in figuring out the below solution.

Thank you Radnip, I will practise writing all my future codes in a separate class.

Below is the enhanced code for deleting records in bulk without the need of having any foreign key.

if(Trigger.isAfter && Trigger.isDelete){

    Set<id> MObjId = New Set<id>();
        List<MasterObjName__c> MObjToDel = new List<MasterObjName__c>();
        for(ChildObjName__c CObj : Trigger.Old){
                    
            MObjId.add(CObj.RelationshipName__c);
        }
                   
        List<MasterObjName__c> MObjLst = new List<MasterObjName__c>();
                    
        MObjLst= [select id,(Select id from ChildObjName __r )From MasterObjName__c where id in :MObjId];
     
        for(MasterObjName__c var:MObjLst){
                    
            if(var. ChildObjName __r.size()==0)
              MObjToDel.add(var);
        }
                        
        if (MObjToDel.size()>0)
                       
            delete MObjToDel;

}

All Answers

Balaji BondarBalaji Bondar
Hi Srish,

Try below code:
if(Trigger.isAfter && Trigger.isDelete){
	Set<MasterObjName__c   MasterObjNameIdSet = new Set<MasterObjName__c ();
	for(MasterObjName__c Mobj : [Select Id, RelationshipName__c from  MasterObjName__c  where RelationshipName__c IN:Trigger.Old]){
		MasterObjNameIdSet.add(Mobj.RelationshipName__c);
	}
	delete MasterObjNameIdSet;	
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
RadnipRadnip
BTW "best practise" is to have your code outside the trigger in a class then call the class method from the trigger. Keeps the trigger nice and clean on the object. More tips checkout Apex ten commandments on youtube: https://www.youtube.com/watch?v=D7mqMYliy3A
srishsrish
Thank you Balaji Bondar for the code, it rellay helped me in figuring out the below solution.

Thank you Radnip, I will practise writing all my future codes in a separate class.

Below is the enhanced code for deleting records in bulk without the need of having any foreign key.

if(Trigger.isAfter && Trigger.isDelete){

    Set<id> MObjId = New Set<id>();
        List<MasterObjName__c> MObjToDel = new List<MasterObjName__c>();
        for(ChildObjName__c CObj : Trigger.Old){
                    
            MObjId.add(CObj.RelationshipName__c);
        }
                   
        List<MasterObjName__c> MObjLst = new List<MasterObjName__c>();
                    
        MObjLst= [select id,(Select id from ChildObjName __r )From MasterObjName__c where id in :MObjId];
     
        for(MasterObjName__c var:MObjLst){
                    
            if(var. ChildObjName __r.size()==0)
              MObjToDel.add(var);
        }
                        
        if (MObjToDel.size()>0)
                       
            delete MObjToDel;

}
This was selected as the best answer