Salesforce Free Developer >

Chapter 8 - Exception Handling >

Exceptions in APEX Triggers

Exceptions in APEX Triggers

Triggers can be used to prevent DML operations from occuring 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:

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.

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners. But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *