function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raghavendra Sharma 12Raghavendra Sharma 12 

hi i am new in salesforce, my question is there are two custom object with a lookup relationship parent and child, and i want when parent record is delete all the child record is delete which is associated with that parent record

trigger deletes on parent__c (before delete) {
public set<id> h;
for(parent__c d:trigger.old)
{

h.add(d.name);
}

list<child__c> d=[SELECT name FROM child__c where link__c =: h ];
  
system.debug(d);
delete d;

}
Jörn Flath 15Jörn Flath 15

Hi,
if you use Master-Detail-Relationship (instead of a normal lookup), the deletion of the child happens automatically. You don't have to write an Apex-Trigger for that.

https://help.salesforce.com/articleView?id=overview_of_custom_object_relationships.htm&type=5

pradeep kumar yadavpradeep kumar yadav
If you get solution marked it as best answer to help others easily find their solution
trigger deletes on parent__c (before delete) {
set<id> h = new set<id>();
for(parent__c d:trigger.old)
{

h.add(d.Id);
}

list<child__c> d=[SELECT name FROM child__c where link__c IN : h ];
 
if(d != null && d.size() > 0)
    delete d;

}