229k views
0 votes
To prevent saving records in a trigger, call this method, which throws a fatal error inside a trigger. The error message is displayed in the user interface and is logged.

User Krishna V
by
7.8k points

1 Answer

2 votes

Final answer:

The addError() method in Salesforce can be used to prevent saving records in a trigger and display an error message.

Step-by-step explanation:

To prevent saving records in a trigger and display an error message, you can use the addError() method in Salesforce. This method adds an error to a record and prevents it from being saved. The error message can be displayed in the user interface and logged for reference. Here is an example:

// Create a new Account
Account acc = new Account(Name='Test Account');
// Add an error to the Account
acc.addError('Unable to save Account record');
// The Account record will not be saved
Database.insert(acc, false);

User Djburdick
by
8.1k points