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
MokshadaMokshada 

I have lookup relationship between 2 objects. if i delete the parent record i want to delete the child record also .how can i do this

I have lookup relationship between 2 objects on both objects if i delete the parent record i want to delete the child record also .how can i do this.
I was trying to do this but it is not getting deleted can anyone help where I am doing wrong.can anyone help

trigger TriggerOnTeacher on Teacher__c (after insert, after update, before delete) {
    List<Student__c> studentRecordsToUpdate = new List<Student__c>();
    List<Student__c> studentRecordsToDelete = new List<Student__c>();
    Set<Id> studentIdsToDelete = new Set<Id>();
    if (Trigger.isUpdate) {
        for (Teacher__c teacherRecord : Trigger.new) {
            Student__c studentRecord = [SELECT Id, Name, Firstname__c, Lastname__c, Email__c, Mobile__c, DOB__c, Address__c, City__c, Country__c
                                        FROM Student__c
                                        WHERE Teacher__c = :teacherRecord.Id];
            if (studentRecord != null) {
                studentRecord.Name = teacherRecord.Name;
                studentRecord.Firstname__c = teacherRecord.Firstname__c;
                studentRecord.Lastname__c = teacherRecord.Lastname__c;
                studentRecord.Email__c = teacherRecord.Email__c;
                studentRecord.Mobile__c = teacherRecord.Mobile__c;
                studentRecord.DOB__c = teacherRecord.DOB__c;
                studentRecord.Address__c = teacherRecord.Address__c;
                studentRecord.City__c = teacherRecord.City__c;
                studentRecord.Country__c = teacherRecord.Country__c;
                studentRecordsToUpdate.add(studentRecord);
            }
        }
    }
    
    if (Trigger.isInsert) {
        for (Teacher__c teacherRecord : Trigger.new) {
            if(triggerCount.runonce()){
                Student__c studentRecord = new Student__c();
                studentRecord.Teacher__c = teacherRecord.Id;
                studentRecord.Name = teacherRecord.Name;
                studentRecord.Firstname__c = teacherRecord.Firstname__c;
                studentRecord.Lastname__c = teacherRecord.Lastname__c;
                studentRecord.Email__c = teacherRecord.Email__c;
                studentRecord.Mobile__c = teacherRecord.Mobile__c;
                studentRecord.DOB__c = teacherRecord.DOB__c;
                studentRecord.Address__c = teacherRecord.Address__c;
                studentRecord.City__c = teacherRecord.City__c;
                studentRecord.Country__c = teacherRecord.Country__c;
                studentRecordsToUpdate.add(studentRecord);
            }
        }
    }
    
    if (Trigger.isDelete) {
        for (Teacher__c teacherRecord : Trigger.old) {
            studentIdsToDelete.add(teacherRecord.Student__c);
        }
    }
    
    if (!studentRecordsToUpdate.isEmpty()) {
        upsert studentRecordsToUpdate;
    }
    
    if (!studentRecordsToDelete.isEmpty()) {
        List<Student__c> studentsToDelete = [SELECT Id FROM Student__c WHERE Teacher__c IN :studentIdsToDelete];
        delete studentsToDelete;
    }
}

Thanks,
VinayVinay (Salesforce Developers) 
Hi Mokshada,

You can use cascade delete feature to delete child records whenever parent records are been deleted.

https://help.salesforce.com/s/articleView?id=000382017&type=1

Else you can check below example.
https://developer.salesforce.com/forums/?id=9060G000000Xd1iQAC

Please mark as Best Answer if above information was helpful.

Thanks,
Arun Kumar 1141Arun Kumar 1141
Hello Mokada,

To automatically delete child records when the parent record is deleted, you can use the Salesforce declarative feature called "Cascade Delete" or create a trigger using Apex.

To learn more about cascade delete check this out.
https://help.salesforce.com/s/articleView?id=000380112&type=1

Thanks.
MokshadaMokshada
Hi,
but I wanted to do it via trigger but it's not working can you please help me in identifying what I am doing wrong.

thanks.