October 26, 2014

Deleting child records automatically when parent records are deleted using Lookup Relationship

Description

Generally we have relationships in Salesforce i.e. Master-Detail and Lookup Relationships. The main difference between these two relationships is when we delete parent record the child record will delete automatically in Master-Detail but not in Lookup. Now the scenario is, by using Lookup relationship also the records should delete as in Master-Detail. This we can achieve by writing trigger on Parent Object.

Steps for the scenario

Step 1: Create Custom Object,Fields and Records

Object Name : Parent__c



Step 2 : Create Custom Object,Fields and Records
Object Name : Child__c



Step 3 : Create Lookup Relationship between the two objects(i.e. Parent and Child) which you have created.



Step 4 : Write Trigger on Parent Object. Check the active check box



Step 5 : Delete the record in Parent object and check the child object records

Output:






Interview Questions

  • What is the difference between Master-Detail and Lookup Relationship?
  • What is the relationship between Account and Contact objects?
  • What is Trigger and its syntax ?
  • How many events present in Trigger?
  • What is the difference between Trigger.new and Trigger.old?
  • Why we are using before event in this Trigger?
  • Why we are using Trigger.old in this scenario?

No comments:

Post a Comment