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.


2 comments :

  1. Hi Loresh, quick question on this. My use case is that I want to prevent deletion of an opportunity if it's past the first stage. Could I use something like the my Power of One field (info here, if you're not familiar: https://trailblazers.salesforce.com/answers?id=9063000000048RbAAI) and use that instead of a rollup? That way I could use a formula that said 'if power of one is not 0 and stage is not 0. SQL' throw the error message? What would that formula look like? Thanks for your help!

    ReplyDelete
    Replies
    1. And I apologize Lokesh, for spelling your name wrong!

      Delete