9

Trigger Exceptions

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

salesforce-developer
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book A 15-Minutes Free Career Counselling Today!