function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nikhil SomvanshiNikhil Somvanshi 

Error: There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Cannot Delete".

Hi People,
I am writing a simple code to refrain the user from deleting a record if it's active but getting the below error:

 Error: There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Cannot Delete".

Here is my code:
trigger DontDeleteIfActive on Position__c (before delete) {

if(trigger.isdelete)
{
for (Position__c pos : trigger.old)

{   

    if(pos.Active__c != null || pos.Active__c == True)
        pos.addError('Cannot Delete');
    
}
}

}
 

Best Answer chosen by Nikhil Somvanshi
Alisha Mehta 9Alisha Mehta 9
Hi, 
Put '&&' condition  if(pos.Active__c != null && pos.Active__c == True)
Then it will only delete records that are not active else throws error