You need to sign in to do that
Don't have an account?
How To Display Custom Error Message?
Trigger Scenario:
Write a Trigger on the Account and Check Only Student Admin Profile Users Should Be Able to Delete an Account.
For the above scenario, I have written the following two types of handler classes.
Type 1: It Shows the Custom Error
public class PracticeHandlerClass {
public static void practiceMethod(List<Account> accList) {
for(Account acc : accList) {
if(UserInfo.getProfileId() != 'Student Admin') {
acc.addError('User Not Allowed To Delete The Record');
}
}
}
}
-----------------------------------
Type 2: It Doesn't Show the Custom Error
public class PracticeHandlerClass {
public static void practiceMethod(List<Account> accList) {
Profile userProfile = [SELECT Id, Name FROM Profile WHERE Name = 'Student Administrator'];
if(userProfile != null && UserInfo.getProfileId() != userProfile.Id) {
for(Account acc : accList) {
acc.addError('User Not Allowed To Delete The Record', 'CustomError');
}
}
}
}
Even the 2nd type shows the error but, not the custom error message. I want to understand why?
What would be the correct code that will display the custom error message?
As per this article,https://developer.salesforce.com/docs/atlas.en-us.244.0.apexref.meta/apexref/apex_methods_system_sobject.htm#:~:text=with%20sObjects.-,SObject%20Methods,-The%20following%20are
,which method signature for addError are you trying to pass?