3

I have two custom objects, call them Parent and Child. Child has a Lookup to Parent, via Parent__c. It is not a Master:Detail relation.

What pattern could I use to prevent deletion of a Child record by users, except in cases where the Parent record is being deleted? In other words, I would like to implement a cascade delete, but not allow users to delete Child records themselves.

Should I simply accomplish this by revoking Delete permission on Child, and implement the cascade delete in a trigger on Parent? What is the best practice?

3

I think you can achieve it in two ways including the one you mentioned.

1) As you suggested, remove delete permission on child. It will remove delete option on child. Then write trigger on parent, to delete child also, when parent is deleted. This would be the best approach.

2) Write trigger on both child and parent. child trigger will prevent deletion by record.addError(). Then in parent object trigger, you can delete child records also.

| improve this answer | |
  • Thank you. For option #2, what would I need to do in order to override the trigger on child? Would I want some custom field that I toggle in the Before Delete on Parent, so that the Child trigger could permit the deletion? – Pittsburgh DBA May 28 '15 at 16:57
  • Yeah. You will need to do that. Or there is an option while creating lookup "Clear value in lookup if parent is deleted". Select that. Then in child trigger, if the lookup value is empty, allow deletion. – Victor May 28 '15 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.