anusha(salesforce developer)

Thursday 9 June 2016

senarios

Prerequisite

Child__c has a look-up relationship with the object Parent__c. In case of look-up relationship, if we delete the parent objects records then in child object only that look-up value will be removed. Here the requirement is whenever user delete the parent records then all the related child records should be deleted (entire record).

Solution
  1. /* Description:
  2. * ============
  3. * Apex Trigger to delete the related child object records upon
  4. * deleting the parent object record in case of look up
  5. * relationship *
  6. * Author:
  7. * ========
  8. * www.srinusfdc.com
  9. */
  10. trigger ParentTrigger on Parent__c (before delete) {
  11.     /*Assume that parent__c is a field api name( look up to parent object) in child object.*/
  12.     delete [select id from Child__c where parent__c in: trigger.old];
  13.     /*Note: trigger.old will be automatically converted to set of ids
  14.     by salesforce (alternative: trigger.oldMap.keyset()).*/
  15. }

No comments:

Post a Comment