Some time you may have a requirement whenever Parent is deleted the related Child record should be deleted in Lookup Relationship.
By default, the "lookup" relationship field only having the following options.
In this case you can go for trigger on Parent object with after delete event.
Here is the code,
- trigger DeleteContact on Account (after delete)
- {
- List<Contact> contacts = [SELECT AccountId FROM Contact WHERE AccountId IN:Trigger.OldMap.keyset()];
- delete contacts;
- }
If your have custom object relationship means use Lookup_Field__r.Id in the place of AccountId in the above query.
you didn't use trigger.old if you want to delete records from trigger or How we can identify will it delete or something else?
ReplyDelete