
16 answers
Hi ,I test the same code and working fine. Create a Custom Label Called UserGroup and add userid (18 Digit) in comma separated value if you have multiple. Handler Class
public class CannotDeleteAccount {
public static void NoDeleteAccount (List<Account> accounts){
system.debug('before delete trigger');
String Userid = UserInfo.getUserId();
System.debug('Current User id'+Userid);
set<string> strkey = new set<string>(Label.UserGroup.split(','));
System.debug('strkey'+strkey);
for (Account acc: accounts){
If(strkey.contains(Userid)){
acc.addError('You cannot delete the Account');
}
}
}
}
This should work without any issue. Simply Copy-Paste the above code will work.Call the Method From trigger
Trigger accountTrigger on Account (before delete) {
if(Trigger.isBefore){
System.debug('Inside the Trigger');
CannotDeleteAccount.NoDeleteAccount(Trigger.Old);
}
}