top of page
Writer's pictureN. Mehra

How to Use adderror method in Apex Trigger

Updated: Sep 21, 2023



Using an Apex Trigger, a custom error message can be shown in the UI of Salesforce for the user to see. Doing this can give a user a comprehensive message about what went wrong.


An addError() method is used in the trigger to prevent the record from being inserted or created.


In this addError() method, we can pass a string as a message to the user about what went wrong.


As an example, suppose we need to pass an error message into account.


Account.addError(‘Something went wrong’);


Let’s understand with the help of an example. There is a business requirement where an error needs to be shown when the Account field number of employees is 0 or Null.

We need to write a trigger on the Account to check the number of employees before inserting the Account.


There are three ways to show an error message. Let’s see all those below.


1. Display the error message normally at the bottom.


Trigger -

  • Trigger Name - AccountError

  • Add before insert as per our business requirement

  • Use AccountErrorHandler class to write the logic.

trigger AccountError on Account (before insert) {
AccountErrorHandler.checkNumberOfEmployee(Trigger.New);
}

Handler Class -

  • Class - AccountErrorHandler

  • Create a method name checkNumberOfEmployee() which will check the number of employee is 0 or null.

  • In this method we use addError() if it matches the condition.

public class AccountErrorHandler {
public static void checkNumberOfEmployee(List<Account> accounts){
for(Account account : accounts){
if(account.NumberOfEmployees <= 0 || account.NumberOfEmployees == Null){
                account.NumberOfEmployees.addError('Please enter atleast one employee');
            }
        }
    }

2. Display the error message directly in the field.

  • We just need to change the handler class.

  • We add a field name before the addError() method like this-

Account.NumberOfEmployees.addError('Please atleast enter one employee');


public class AccountErrorHandler {
public static void checkNumberOfEmployee(List<Account> accounts){
for(Account account : accounts){
if(account.NumberOfEmployees <= 0 || account.NumberOfEmployees == Null){
                account.NumberOfEmployees.addError('Please enter atleast one employee');
            }
        }
    }
}


3. Display error message using a custom label-


To create a custom label: Go to Setup > Click on the quick find box > Search for “Custom Label”> Open “Custom Labels”> Click on New Custom Label button



How to use this custom label

You need to save this custom label and now we can use it in our code like this

Example - Label.LabelName

In our case - Label.At_Least_One_Employee

In the handler class, we simply need to pass this label to the addError() method.


public class AccountErrorHandler {
public static void checkNumberOfEmployee(List<Account> accounts){
for(Account account : accounts){
if(account.NumberOfEmployees <= 0 || account.NumberOfEmployees == Null){
                account.NumberOfEmployees.addError(Label.At_Least_One_Employee);
            }
        }
    }
}


Happy!! to help you add to your knowledge. You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.


Blog Credit:

N. Mehra

Salesforce Developer

Avenoir Innovations Pvt. Ltd.




Reach us: team@avenoir.ai

Recent Posts

See All

© 2024 by Avenoir Technologies Pvt. Ltd.

bottom of page