You need to sign in to do that
Don't have an account?
Sourav P
Can merge accounts but shouldn't able to manually delete an account
Hi,
I have a profile, which should able to merge accounts. But They shouldn't manually delete any accounts. Although the delete button in the account has been removed, in the list view its till visible.
can anyone, help me with a trigger , that while they delete, it should through an error message but while merging there shoulnt be an issue. Thanks
I have a profile, which should able to merge accounts. But They shouldn't manually delete any accounts. Although the delete button in the account has been removed, in the list view its till visible.
can anyone, help me with a trigger , that while they delete, it should through an error message but while merging there shoulnt be an issue. Thanks
You can't use a Validation Rule to prevent a Deletion. That has to be a trigger. Please Find the Below Trigger Which Will Prevent User To delete the Account Record.
Below Code, I have used Custom Label to store the Profile Id. If In Future if your org needs to extend this Feature to some other profile then You can add the value only in the custom label without even changing the code.
If you want to add the same logic to User Label then you can add the user id in comma separated value in the label.
Handler Class :
public class CannotDeleteAccount {
public static void NoDeleteAccount (List<Account> accounts){
system.debug('before delete trigger');
String ProfileId = UserInfo.getProfileid();
set<string> strkey = new set<string>(Label.Profile_Group.split(','));
System.debug('Profile Id of Current User'+ProfileId);
for (Account acc: accounts){
If(strkey.contains(ProfileId)){
a.addError('You cannot delete the Account');
}
}
}
}
Trigger :
trigger accountTrigger on Account (before delete) {
if(checkRecursive.runOnce()){
if(Trigger.isBefore){
System.debug('Inside the Trigger');
CannotDeleteAccount.NoDeleteAccount(Trigger.Old);
}
}
}
If this helps Mark this as the best answer.
Let me try it
The handler class, i modified a bit as below as it was showing Invalid identifier error.
The trigger class is also showing the same kind of error as below,
Error: Compile Error: Invalid identifier ' '. Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 - 9), '$', '_'. at line 1 column 50
I am not able to know here, why so. can you plz help me out. Thanks
I wrote the same code of yours in my developer org and I wasn't able to delete an account. Your Code Works Perfectly Fine.
The only thing you missed in defining the method as static. Since the Nonstatic method cannot be referenced from a static context: void.
And also if you are using Check Recursive you have to add the class as well. This is a best practice we use to make sure the trigger doesn't go in recursive.
So Your Code should look something like this.
Let me know if this helps.
Let me try this.