30 Salesforce Apex Trigger Examples – Part 1

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Code  –

Explanation –

Trigger Example 2 –

Your Company wants to ensure that the shipping address of an Account record is always in sync with the billing address

Code –

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.

Code –

Trigger Explanation –

Learn Complete Salesforce Flows from Basic to Advance

Trigger Example 4 –

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.

Code –

Trigger Explanation –

Trigger Example 6 –

Your company wants to ensure that all accounts in their system have unique names. This can help avoid confusion and ensure that data is accurately attributed to the correct account. For example, in a CRM system used by a sales team, duplicate account names can lead to issues when trying to track sales activities and opportunities for each account.

Code –

Trigger Explanation –

Trigger Example 7 –

Suppose a company wants to automatically create a contact for each new or updated account that meets certain criteria. Specifically, they want to create a contact only when a checkbox field “Create_Contact_Checkbox__c” is checked on the account record. In addition, the first name of the contact should be the same as the account name, and the last name should be “Contact”. Finally, the contact should be associated with the account and inherit the account’s phone number.

Code –

Trigger Explanation –

Trigger Example 8 –

Your company wants to keep track of the total amount of opportunities associated with each account in a Salesforce org. This information could be useful for sales and marketing teams to identify high-value accounts and prioritize their efforts accordingly.

Code –

Trigger Explanation –

Trigger Example 9 –

In a company, there are multiple accounts and each account has many opportunities associated with it. Sometimes, the sales representative forgets to close an opportunity even after it has passed the 30-day mark. This creates a negative impact on the company’s sales pipeline and forecasting. The company wants to make sure that all opportunities older than 30 days are closed and marked as ‘Closed Lost’ to keep their sales pipeline clean and accurate.

Code –

Trigger Explanation –

Trigger Example 10 –

Your company ABC Pvt Ltd. wants to prevent users from accidentally deleting active accounts in a Salesforce org. For example, if an account is associated with ongoing sales opportunities or active customer relationships, deleting that account could result in data loss or disrupt business operations.

Code –

Trigger Explanation –

Salesforce Apex Trigger Examples Part 2 

Get a complete Roadmap To Learn Salesforce Admin And Development 👇

Share Now

Shubham is the Founder of Salesforce Geek. He is also a Salesforce Certified Consultant.

Related Posts

One thought on “30 Salesforce Apex Trigger Examples – Part 1

Leave a Reply

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