[SalesForce] Prevent related record deletion, except when parent record is being deleted

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?

Best Answer

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.

Related Topic