Friday 12 September 2014

How to delete child record in Lookup relationship Salesforce


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,

  1. trigger DeleteContact on Account (after delete)
  2. {
  3.     List<Contact> contacts = [SELECT AccountId FROM Contact WHERE AccountId IN:Trigger.OldMap.keyset()];
  4.     delete contacts;
  5. }

If your have custom object relationship means use Lookup_Field__r.Id in the place of AccountId in the above query.

1 comment:

  1. 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

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...