- Published on
How to Create an Apex Trigger
- Authors
- Name
- Tony Geiser
Creating an Apex Trigger in Salesforce involves several steps. Here’s a detailed guide to help you through the process, from initial setup to deployment.
Step-by-Step Guide to Creating an Apex Trigger
1. Identify the Requirements
Before you start coding, clearly define what you want your trigger to accomplish. Determine:
- The Salesforce object that the trigger will act upon (e.g., Account, Contact).
- The event(s) that will cause the trigger to execute (e.g., before insert, after update).
- The specific logic or actions you need to perform.
2. Prepare Your Development Environment
You can create and edit Apex Triggers directly in Salesforce using the Developer Console or through an Integrated Development Environment (IDE) like Visual Studio Code with Salesforce extensions.
Using Developer Console:
- Open Salesforce.
- Click on the gear icon and select Developer Console.
Using Visual Studio Code:
- Install Visual Studio Code and the Salesforce Extensions Pack.
- Set up Salesforce CLI and connect it to your Salesforce org.
- Create or open a Salesforce DX project.
3. Create the Trigger
In the Developer Console or your IDE, you’ll create a new trigger file. Follow these steps:
In the Developer Console:
- Open the Developer Console: Click on the gear icon in Salesforce and select Developer Console.
- Create a New Trigger:
- Go to File > New > Apex Trigger.
- Select the Salesforce object for which you want to create the trigger (e.g., Account).
- Enter a name for your trigger (e.g.,
AccountTrigger
). - Click Submit.
In Visual Studio Code:
- Open your Salesforce DX Project.
- Create a New Trigger:
- Right-click on the
triggers
folder in your project. - Select New File and name it (e.g.,
AccountTrigger.trigger
).
- Right-click on the
4. Define the Trigger Logic
Write the logic for your trigger. Here’s a basic template and an example:
Basic Template:
trigger TriggerName on ObjectName (before insert, after update) {
if (Trigger.isBefore) {
if (Trigger.isInsert) {
// Your logic for before insert
}
if (Trigger.isUpdate) {
// Your logic for before update
}
}
if (Trigger.isAfter) {
if (Trigger.isInsert) {
// Your logic for after insert
}
if (Trigger.isUpdate) {
// Your logic for after update
}
}
}
Example: Here’s an example of a simple trigger on the Account
object that sets a default value for a custom field Status__c
before the record is inserted:
trigger AccountTrigger on Account (before insert, after update) {
if (Trigger.isBefore) {
if (Trigger.isInsert) {
for (Account acc : Trigger.new) {
if (acc.Status__c == null) {
acc.Status__c = 'New';
}
}
}
}
if (Trigger.isAfter) {
if (Trigger.isUpdate) {
// Logic for after update, such as updating related records
for (Account acc : Trigger.new) {
if (acc.Industry != Trigger.oldMap.get(acc.Id).Industry) {
// Custom logic here
}
}
}
}
}
5. Save and Deploy the Trigger
- In Developer Console: Click the Save button.
- In Visual Studio Code: Deploy the trigger using the Salesforce CLI or VS Code commands (e.g., right-click on the file and select Deploy Source to Org).
6. Test Your Trigger
Before deploying to production, thoroughly test your trigger in a sandbox environment to ensure it behaves as expected.
Create Test Classes:
- Write an Apex test class to cover your trigger logic with test methods that validate the behavior.
- Use assertions to verify that the trigger performs the correct actions.
Example Test Class:
@isTest
public class AccountTriggerTest {
@isTest
static void testAccountTrigger() {
// Create a test account
Account testAccount = new Account(Name = 'Test Account');
insert testAccount;
// Verify the trigger logic
Account insertedAccount = [SELECT Status__c FROM Account WHERE Id = :testAccount.Id];
System.assertEquals('New', insertedAccount.Status__c, 'Status should be set to New');
}
}
Run Tests:
- In the Developer Console, go to Test > New Run and select your test class.
- In Visual Studio Code, use the Run Tests command from the Command Palette or right-click on the test class.
7. Deploy to Production
Once the trigger is thoroughly tested, you can deploy it to your production environment.
Using Change Sets:
- Go to Setup > Change Sets in Salesforce.
- Create a new Outbound Change Set.
- Add your trigger and any related components.
- Upload the Change Set to your production environment and deploy it.
Using Salesforce CLI:
- Use the
sfdx force:source:deploy
command to deploy your trigger directly from your local project.
Using Metadata API:
- Package your trigger in a deployment package and deploy it using the Metadata API tools.
Best Practices
- Bulkify Your Code: Ensure your trigger can handle multiple records efficiently.
- Avoid Recursive Calls: Use static variables or frameworks to prevent triggers from recursively calling themselves.
- Use Helper Classes: Encapsulate business logic in separate Apex classes called by the trigger to keep the trigger code clean and maintainable.
- Test Coverage: Write comprehensive test cases to cover your trigger logic and achieve high code coverage.
Creating an Apex Trigger involves understanding the business requirements, setting up your development environment, writing and testing the trigger code, and deploying it following best practices. By adhering to these steps, you can ensure that your triggers are effective and maintainable.
As always, please refer to the official Salesforce documentation here.