Pages

Monday 19 August 2019

Validation rule to prevent the deletion of Record

Salesforce does not give any function to check for delete events in Validation rule e.g. ISDELETE() which could be similar to ISNEW().

There are many ways to achieve this, I know you would be thinking to write a trigger on before delete event and throw the error message. Yes, your thinking is correct, However, this can also be achieved without writing code. #powerofadmin

Let's consider a scenario. 
Opportunity line items cannot be deleted if the opportunity stage is closed-won.

1. Create new Roll-up summary field on Opportunity Object. This field will be used to count the total number of Opportunity Products on the Opportunity.


2. Create Validation rule on the Opportunity object. This rule will prevent the record delete if the Opportunity Stage is "Closed Won". 

Result:

Note: The above solution works for Master-Detail relationship between two objects.