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
Nani SircillaNani Sircilla 

After delete trigger not firing on master object

Hi All,

I have two custom objects lke Student__c(Detail) and Batch__c(Mater). Now whenever delete Batch__c record then delete associated detail records. i have created After Delete trigger on Batch__c with Master-Detail relationship on Student__c. But After trigger not firing on child and just simply delete master records only.
So what should i  do to delete Detail records whenever delete Master record.
 

trigger deleteBatch on Batch__c (After delete) {
    for(Batch__c bt:trigger.old){
    List<Student__c> stu  = [SELECT id FROM Student__c WHERE Id =: bt.id];
    delete stu;
    }   
}


Now my Question is: 1). Which relationship is required on Student__c whether it is Lookup or Master.
2). Can we delete Detail records whenever delete Master record using Lookup field on Detail object.
3) is there any relationship field required on Master object.

Please any one give me answers.

Thank you..
Nani
 

Shailesh DeshpandeShailesh Deshpande
  1. If you have a Master-Detail relationship, child records will get deleted automatically when Master record is deleted. You do not need a trigger in this case.
  2. If you have a Lookup relationship, you can specify one of three behaviors to occur if the lookup record is deleted:
  • Clear the value of this field" This is the default. Clearing the field is a good choice when the field does not have to contain a value from the associated lookup record.
  • "Don’t allow deletion of the lookup record that’s part of a lookup relationship" This option restricts the lookup record from being deleted if you have any dependencies, such as a workflow rule, built on the relationship.
  • "Delete this record also" Available only if a custom object contains the lookup relationship, not if it’s contained by a standard object. However, the lookup object can be either standard or custom. Choose when the lookup field and its associated record are tightly coupled and you want to completely delete related data.





 
William LópezWilliam López
Hello Nani,

Here some answers, I hope it helps:

1). Which relationship is required on Student__c whether it is Lookup or Master.

There are 2 tipes of relationship, Lookups are optionals and Master-Detail are required. 

Lookup its just a link between two objects, Master-Detail means the detail record cannot exits by itselft.

If you have a Master-detail relationship from Student__c to Batch__c, there is not need of a trigger, Salesforce its going to delete the children records automatically once parent "master" its deleted.

If you have a Lookup relationship from Student__c to Batch__c, when Batch__c its deleted, students are NOT going to be deleted, you are going to need a batch.

Therefore in your case its better to use Master-detail.

2). Can we delete Detail records whenever delete Master record using Lookup field on Detail object.

When using lookup there is not "master" in the relation, you have "Parent" and "Child" therefore there are independents.

You will nned a trigger, but there is a couple of changes that need to be done in you trigger, you need to buklify it and you change the where condition insted of using  Id =: bt.id.  it should be Batch__c =: bt.id.

Here is the trigger with some changes, but again if you need this logic do not use a lookup, usea master-detail and you will save some coding.
 
trigger deleteBatch on Batch__c (After delete) {
//This way we get all records in a single SOQL instead of deleting one by one

        List<Student__c> stu  = [SELECT id FROM Student__c WHERE Batch__c  IN :Trigger.newMap.keySet()];

        delete stu;
}

PS: I am assuming that the field in the student its call "Batch__c"

3) is there any relationship field required on Master object.
I did not understood this one. But In general Master do not need any required field, just a Name

But the best way to check this its with the salesforce details:

User-added image

Please let me know how it goes.

Regards,

Bill

​Don't forget to mark your thread as 'SOLVED' with the answer that best helps you. 

 
Tejpal KumawatTejpal Kumawat
Hello Nani Sircilla,

Your object relationship looks like : lookup relationship.

Try this code
trigger deleteBatch on Batch__c (After delete) {
    if (Trigger.isAfter) {
        if (Trigger.isDelete) {
            List<Student__c> stu  = [SELECT id FROM Student__c WHERE Batch__c = null];
            if(stu.size() > 0)
                delete stu;
        }
    }
}
If code not save in your org then Replace Field Api in line no 4 from Batch__c to your org Field API Name of Batch field from student object.

If this answers your question then hit Like and mark it as solution!
 
SYED MOOSA NAZIR T NSYED MOOSA NAZIR T N

Hi Nani Sircilla,

You need to go with BEFORE DELETE event.
The reason you are facing this challenge is do to the followings.
In AFTER DELETE Trigger, The Parent ID will get removed from the Child records lookup field. To cross verify this, put a debug statement after the SOQL query. you will get zero records in the List.
Also I would suggest you to remove the SOQL and DML statements inside the FOR loop as it will fail in large volume of transcations. 

Thanks
TN Syed Moosa Nazir
smartmoosa@gmail.com | Salesforce Developer | Wipro Technologies