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
Scott ShoningScott Shoning 

Need help with an Apex class/trigger to selectively prevent account deletion

We have our Account triggers set up as one single apex class, which makes it challenging to find code suggestions as I am an Apex newb.

My requirements are as follows:

I need to restrict the deletion of account records to System Admins only, when the value of a certain checkbox field is set to true on the account.  
My research tells me I need a trigger, but since we don't have individual triggers I need to do this in our AccountTriggerHandler apex class.

Any advice is appreciated!
Best Answer chosen by Scott Shoning
Srinivas SSrinivas S
public class AccountTriggerHandler {
	public static void beforeDelete(List<Account> oldAccounts) {
		for(Account acc : oldAccounts) {
			if(acc.yourcheckbox__c && [maintain profiles in hierarchy custom settings]) {
				acc.addError('your error message');
			}
		}
	}
}

-----------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.

All Answers

Srinivas SSrinivas S
public class AccountTriggerHandler {
	public static void beforeDelete(List<Account> oldAccounts) {
		for(Account acc : oldAccounts) {
			if(acc.yourcheckbox__c && [maintain profiles in hierarchy custom settings]) {
				acc.addError('your error message');
			}
		}
	}
}

-----------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
This was selected as the best answer
Scott ShoningScott Shoning
I was able to take what you provided and make it work with our environments.  Thanks for the suggestion!