Experience Salesforce
Trigger Exceptions
What You’ll Learn
- Trigger Exceptions
Topics
- Basics of Triggers
- When to use triggers
- Types of Apex Triggers in Salesforce
- Trigger Order of Execution
- Trigger Context Variables in Salesforce
- Trigger.new in Salesforce| Context Variables in Apex Triggers
- Trigger.old in Salesforce | Context Variables in Apex Triggers
- Trigger.newMap in Salesforce| Context Variables in Apex Triggers
- Trigger.oldMap in Salesforce| Context Variables in Apex Triggers
- Trigger Exceptions
- Best Practice in Triggers
- Trigger Helper Class Pattern
Trigger Exceptions
Triggers can be used to prevent DML operations from occurring by calling the addError() method on a record or field. When used on trigger.new records in insert & update triggers and on Trigger.old records in delete triggers,the custom error message is displayed in the application interface & logged to.
Example Of Trigger Exceptions
Trigger AccountDeletion on Account(before delete) {
for (Account a: [SELECT id FROM Account where id in (SELECT AccountId from opportunities) AND id in : Trigger.old]) {
Trigger.oldMap.get(a.id).addError(‘Cannot delete account with related opportunities.’);
}
}
Note:
If a trigger even throws an unhandled exception, all records are marked with an error & no further processing takes place
Next TopicNeed Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.