In this article, we are going to cover 30 real-time Salesforce Apex trigger examples that will help a beginner to learn and practice triggers so that you can improve your Salesforce Apex Trigger skillset.
Note – Now as we have record-triggered flows in Salesforce, flows become so powerful and because of that we can solve a lot more examples that you are going to see in this article with flows as well, so take these examples for practicing and build the preferred solution.
for example, if a business logic can be implemented with the declarative tools in this case Flow, Salesforce recommends going with a declarative approach over coding/ programming.
Before going forward let’s understands the basic of Triggers.
What is Apex Trigger?
Apex triggers in Salesforce are pieces of code that execute before or after specific events occur in the system, such as record creation, update, or deletion. Here are some common scenarios where Apex triggers can be used:
Validation and Data Integrity: Apex triggers can enforce data validation rules and ensure data integrity by performing custom validation logic before records are saved. For example, you can validate that certain fields are filled out correctly or check for duplicate records.
Workflow and Process Automation: Apex triggers can automate complex business processes and workflows by updating related records, performing calculations, or triggering external actions. For example, you can automatically assign records to specific users, update related records, or send notifications when specific conditions are met.
Custom Business Logic: Apex triggers allow you to implement custom business logic and perform actions that are not supported by declarative tools. For example, you can calculate and update a custom score based on different fields in a record, perform complex calculations, or apply custom logic to determine field values.
Integration with External Systems: Apex triggers can be used to integrate Salesforce with external systems by making API calls or performing data synchronization. For example, you can update records in an external database, send data to a third-party system, or retrieve data from an external system when certain events occur in Salesforce.
Record Archiving and Cleanup: Apex triggers can be used to archive or delete records based on specific criteria. For example, you can automatically archive or delete records that are older than a certain date, or move records to a different object for historical purposes.
Audit and Compliance: Apex triggers can be used to enforce audit and compliance requirements by logging changes made to records, enforcing field-level security, or implementing data access restrictions based on user roles or profiles.
These are just a few examples of scenarios where Apex triggers can be used in Salesforce. Apex triggers provide a powerful way to customize and extend the functionality of Salesforce to meet specific business requirements.
10 Salesforce Apex Trigger Examples –
Trigger Example 1 –
Your company wants to ensure that all new Account records have a valid phone number before they are inserted into the system. This could be important for organizations that rely heavily on phone communication with their customers or need to keep accurate contact information for compliance purposes.
Trigger Example 2 –
Your Company wants to ensure that the shipping address of an Account record is always in sync with the billing address
Trigger Explanation –
Trigger Example 3 –
Suppose you are a sales manager for a company that sells products to other businesses. Your team uses Salesforce to manage customer accounts and contacts, and you want to ensure that all contacts associated with a particular account have the same phone number as the account.
Suppose you are managing a team of sales reps in a company that sells software solutions to businesses. Your team uses Salesforce to manage customer contacts and associated accounts. You want to ensure that any changes made to a contact’s description field are automatically reflected in the associated account’s description field.Code –
Trigger Explanation –
Trigger Example 5 –
Suppose you are a developer in company XYZ now your company wants to keep track of the number of contacts associated with each account in their Salesforce database.