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
Renil RejithRenil Rejith 

Delete a custom object using apex trigger

Hi,
SO I have this custom object called member(master) which is a master object for another custom object dependents(detail). Now when I delete this member  object all its dependent object must be deleted . Can somebody help me with this using a trigger code.
Best Answer chosen by Renil Rejith
Khan AnasKhan Anas (Salesforce Developers) 
Hi Renil,

I trust you are doing very well.

Please try the below code, it is working fine. Kindly modify the code as per your requirement.
 
trigger DeleteChild on Member__c (before delete) {
    List<Id> MemberIds = new List<Id>();
    
    for(Member__c mem : trigger.old) {
        MemberIds.add(mem.Id);
    }  
    
    //Collect all child records related to Parent records
    List<Dependent__c> child = [SELECT Id FROM Dependent__c WHERE Member__r.Id IN :MemberIds];
    //Delete child records
    DELETE child;
}

I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Renil,

I trust you are doing very well.

Please try the below code, it is working fine. Kindly modify the code as per your requirement.
 
trigger DeleteChild on Member__c (before delete) {
    List<Id> MemberIds = new List<Id>();
    
    for(Member__c mem : trigger.old) {
        MemberIds.add(mem.Id);
    }  
    
    //Collect all child records related to Parent records
    List<Dependent__c> child = [SELECT Id FROM Dependent__c WHERE Member__r.Id IN :MemberIds];
    //Delete child records
    DELETE child;
}

I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Renil RejithRenil Rejith
Hi, Khan Anas I’m a fresher And It’s been just 4 days working on salesforce ... your answer was a great help ... thank u so much!!! Hoping to get more help from you!!!! Thank u once again!!
Khan AnasKhan Anas (Salesforce Developers) 
Hi Renil,

It's my pleasure. I’m glad I was able to help!

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue. *(After you choose the best answer the question is marked as “resolved”)*

May the (Sales)force be with you :)
Happy Learning!

Regards,
Khan Anas
Renil RejithRenil Rejith
Hi, I have a scenario... can u please help me with a trigger code on insert and update So the scenario is something like this 1. I have two standard objects called Account(company) and Contact(employee). 2. There is another custom object called Salary which has two fields status (active and inactive) and salary amount. 3 . Salary and contact has a look up relation. 4. So, each time I insert or update the salary amount field of an active employee, the total sum of the employee’s salary should be displayed in a field of account object !!! Please help me with a trigger code for this !!! Thank u ! PFA