Apex Trigger Guide

In this post, we will understand how Apex triggers work in Salesforce with a simple example.

Example Trigger


trigger AccountErrorTrigger on Account (before insert, before update) {

    for (Account acc : Trigger.new) {

        if (acc.Name != null && acc.Name.contains('ERROR')) {
            acc.addError('Name cannot contain ERROR');
        }
    }
}
  

This trigger prevents saving records where the Account name contains the word "ERROR".

⬅ Back to Blog