User Story
As a Salesforce administrator, I want to prevent users from accidentally deleting important account records, so that our data remains accurate and up-to-date.
Acceptance criteria:
- When a user attempts to delete an account with a "Hot" rating, annual revenue greater than 1 million, and at least one contact associated with it, they should see an error message preventing them from deleting the record.
Code
Apex Trigger
trigger PreventAccountDeletion on Account (before delete) {
for (Account acc : Trigger.old) {
if (acc.Rating == 'Hot' && acc.AnnualRevenue > 1000000 && [SELECT COUNT() FROM Contact WHERE AccountId = :acc.Id] > 0) {
acc.addError('Cannot delete this account - it has a hot rating, annual revenue more than 1 million, and at least one contact.');
}
}
}
Video
Video does not exists.