You need to sign in to do that
Don't have an account?
Srish
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
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
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
Try below code: 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.
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;
}