User Story
When a user attempts to delete an Account record, a trigger should be fired on the Account object. The trigger should first check if the Active status on the Account is set to 'Yes'. If it is, the trigger should prevent the deletion of the Account record and display an error message to the user informing them that Active Accounts cannot be deleted.
Code
Apex Trigger
trigger PreventActiveAccountDeletion on Account (before delete) {
PreventActiveAccountDeletionHandler.handle(Trigger.old);
}
Apex Handler Class
public class PreventActiveAccountDeletionHandler {
public static void handle(List<Account> oldAccounts) {
for (Account acc : oldAccounts) {
if (acc.Active__c == 'Yes') {
acc.addError('Active Accounts cannot be deleted.');
}
}
}
}
Video
Video does not exists.