We create validation rules in Salesforce to show errors on the record detail page. When a user enters incorrect data, the validation rule stops the save and displays an error message.
But when working with Apex, sometimes validation rules are not enough. For example, we need to use Apex code to check complex conditions or compare multiple records.
In Apex, we use the addError() method to stop the record from saving and show an error message. This works like a validation rule error, but gives you more control inside the code.
In this tutorial, we will learn about the addError() Method in Salesforce Apex. I will explain where and how to use addError() in real-time business scenarios using Apex code.
What is the addError() Method in Salesforce Apex?
The addError() method displays a custom error message in Salesforce Apex and prevents any DML operation from occurring. Another method, addError(fieldName, errorMsg), dynamically adds errors to fields of an sObject associated with the specified field name.
addError() is mainly used in Apex classes, Triggers, Validation logic, and Custom Business Rules to display custom error messages directly on specific fields or records.
Syntax: addError(fieldName, errorMsg)
Account acc = new Account( Name=’Test Account’ );
acc.addError( ‘Name’, ‘Invalid Account Name’ );
Here, we declare the account sObject. Then, we added an error message to the Name field using the addError() method with a custom message that we want to display.
Why Use addError() in Salesforce Apex?
We can use the addError() method for the following purpose:
- It prevents incorrect data or values that do not match the defined condition.
- We can display the custom error message to the user explaining why the save failed.
- It works before the record is saved, so data quality is maintained.
Real-Time Examples to Use the addError() Method in Salesforce Apex
Now I will explain some real-time examples where we can use the addError() method to display a custom error message on the field or record page.
Example 1: Display Error Message at Bottom of Record Page
Let’s take an example: We have created an account record in the account object. If we try to change the account number field value of that existing account record, we need to throw an error message.
We checked the old and new account record IDs in the Apex trigger below. If the new account number is not equal to the old one, then that number is changed. So, here, we want to prevent that record from being saved.
For that, we added the addError() method, which displays error messages in Salesforce UI and prevents records from being saved. Provide the message that you want to display when the error occurs.
After that, save the Apex trigger code to activate it.
trigger PreventFieldChange on Account ( before update ) {
for ( Account oldAcc : Trigger.Old ) {
for ( Account newAcc : Trigger.New ) {
if ( oldAcc.Id == newAcc.Id &&
oldAcc.AccountNumber != newAcc.AccountNumber) {
newAcc .addError ( 'You cannot change the Account Number.' );
}
}
}
}
Navigate to the account object tab and open any existing account record in edit mode.
