You need to sign in to do that
Don't have an account?

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
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.
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:
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.
Your object relationship looks like : lookup relationship.
Try this code 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!
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