+ Start a Discussion
Priyanka7Priyanka7 

Hi,How to delete a parent record when child has no records using triggers

Lokesh KumarLokesh Kumar
can you please explain in detail it is not clear. 

Thanks !
LBKLBK
You need to add an After Delete trigger in the Child Object.

This trigger will have to check if the Master Record has anymore children attached to it. If not, delete it.
Sree SalesforceSree Salesforce
HI Pryanka,

After delete Event :- you can delete parent records.
Trigger you can write on parent object . If you are not not finding any child records on parent record . You can delete it.
But if you write a trigger on child object . How you will get the parent reocrd if the parent record have no child records. 
So write a trigger on parent. Delete it there only.

Let me Know if it helps you and mark it as best answer if my answer helps you so this so;ution usefull for others
LBKLBK
Hi Sree,
You need an event to fire the trigger. It doesn't make sense to write the After Delete trigger in Parent object.
On delete of a Parent record, why do we need to check if Child object has related data?

An After Delete trigger in child object on the other hand, can fetch the Parent Record id from Trigger.Old object and remove Parent if the deleted child is the last of its kind.