Validation Rule to Prevent Deletion of Child in Master-Details

You have 2 custom objects, Job Applications and Reviews that have a master-detail relationship. Users should NOT be allowed to delete review records after job application records have been approved. How would a developer meet this requirement?

Ans – Use a validation rule in conjunction with a roll-up summary field

Validation rule will fire only on Insert or Update operation but not on Delete. But in Master-Details when you have a roll-up summary field, the delete in Child will be update the Master and will fire the Validation rule in Master object.

Validation Rule –

AND( PRIORVALUE( Number_of_Reviews__c ) > Number_of_Reviews__c , TEXT(Picklist__c ) == ‘Approved’ )

If you delete the Child record(i.e. the review record) it will delete the record and will initiate the rollup summary field calculation in the Master object record(i.e Job Application) which is an update in Master, so it will fire the Validation  rule of Master object and will prevent the child record to be deleted from the system. This all operation happen in a single execution.

You can follow the Execution order as below –

1. Old record loaded from database (or initialized for new inserts)
2. New record values overwrite old values
3. System Validation Rules (If inserting Opportunity Products, Custom Validation                          Rules will also fire in addition to System Validation Rules)
4. All Apex before triggers (EE / UE only)
5. Custom Validation Rules
6. Record saved to database (but not committed)
7. Record reloaded from database
8. All Apex after triggers (EE / UE only)
9. Assignment rules
10. Auto-response rules
11. Workflow rules
12. Processes
13. Escalation rules
14. Parent Rollup Summary Formula value updated (if present)
15. Database commit
16. Post-commit logic (sending email)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s