How to Activate a Trigger in Salesforce
- Sophie Ricci
- Views : 28,543
Table of Contents
Salesforce triggers are one of the most powerful tools in the platform — and one of the most misunderstood.
When they work, they quietly automate thousands of operations behind the scenes. When they don’t, they silently kill your data workflows and leave your team wondering why records aren’t updating.
This guide breaks down exactly how to activate a trigger in Salesforce, how to control it properly, and what best practices separate clean trigger architecture from a system that fights itself.
What Is a Salesforce Trigger
A Salesforce trigger is a piece of Apex code that fires automatically when a specific data event occurs — like creating, updating, deleting, or undeleting a record.
There are two types:
- Before triggers — run before the record is saved to the database. Used to validate or modify data before it commits.
- After triggers — run after the record is saved. Used to access system-set fields like record IDs, or to update related records.
Triggers can be associated with standard objects (like Leads, Contacts, Accounts) or custom objects.
According to Salesforce’s own developer documentation, triggers support the following DML events: insert, update, delete, merge, upsert, and undelete — giving you granular control over exactly when your logic fires.
Why Activating Triggers the Right Way Matters
A poorly written or improperly activated trigger can cause:
- Recursive loops that hit governor limits
- Data corruption across related records
- Unexpected failures in critical workflows
- Performance degradation on high-volume orgs
Salesforce governor limits enforce a ceiling of 150 DML statements per transaction and 100 SOQL queries per synchronous transaction. If your trigger isn’t built with bulk handling in mind, you’ll hit these limits fast — especially at scale.
Getting activation right from the start saves you from painful debugging later.
How to Create and Activate a Trigger in Salesforce
Step 1 — Open the Developer Console
Log into your Salesforce org. Click the gear icon (⚙️) in the top-right corner, then select Developer Console. This is your primary environment for writing and activating Apex triggers.
Alternatively, you can use VS Code with the Salesforce Extensions Pack if you’re working in a team environment with version control.
Step 2 — Create a New Trigger
In the Developer Console:
- Go to File → New → Apex Trigger
- Name your trigger (e.g., AccountTrigger)
- Select the sObject it will run on (e.g., Account, Lead, Contact)
- Click Submit
A template is generated automatically. It looks like this:
trigger AccountTrigger on Account (before insert, after insert) {
// Your logic here
}
The event list in the parentheses is what activates the trigger for those specific operations.
Step 3 — Define Your Trigger Events
This is where you tell Salesforce when the trigger should fire. You can combine multiple events:
trigger ContactTrigger on Contact (
before insert,
before update,
after insert,
after update,
before delete,
after delete,
after undelete
) {
// handler logic
}
Choosing the right event matters. Use before insert to set default field values. Use after insert when you need the record’s ID to create related records.
Step 4 — Write Your Trigger Logic
Best practice is to keep the trigger body thin and delegate all logic to a handler class. This is called the Trigger Handler Pattern and it’s the standard approach in professional Salesforce development.
trigger ContactTrigger on Contact (before insert, after insert) {
ContactTriggerHandler handler = new ContactTriggerHandler();
if (Trigger.isBefore && Trigger.isInsert) {
handler.onBeforeInsert(Trigger.new);
}
if (Trigger.isAfter && Trigger.isInsert) {
handler.onAfterInsert(Trigger.new);
}
}
This keeps your trigger file clean and makes your handler class independently testable.
Step 5 — Save and Activate the Trigger
Once you save the trigger in Developer Console, it is active immediately by default. Salesforce does not have a separate “activate” button for triggers like it does for Flows.
However, it will only compile and save successfully if:
- Your Apex code has no syntax errors
- Your org meets code coverage requirements (at least 75% code coverage across all Apex is required in production)
If you’re deploying to production via a Change Set or Metadata API, the deployment will run all existing tests and enforce the 75% coverage rule before the trigger goes live.
How to Activate or Deactivate a Trigger Without Deleting Code
Sometimes you need to turn a trigger off temporarily — for a data migration, a bulk import, or an emergency fix — without deleting the code itself.
The professional approach is to build a Custom Setting toggle.
Build a Toggle with Custom Settings
- Go to Setup → Custom Settings → New
- Create a Hierarchy Custom Setting called TriggerSettings
- Add a checkbox field: Disable_Contact_Trigger__c
Then in your trigger:
trigger ContactTrigger on Contact (before insert, after insert) {
TriggerSettings__c settings = TriggerSettings__c.getInstance();
if (settings.Disable_Contact_Trigger__c) return;
// normal trigger logic
}
Now you can flip a checkbox in Setup to disable the trigger without touching code or deploying anything. This is especially useful for admins managing large data loads.
How to Check If a Trigger Is Active in Salesforce
You can view all existing triggers in your org by going to:
Setup → Apex Triggers (search “Apex Triggers” in Quick Find)
Here you’ll see:
- Trigger name
- Associated object
- Active/Inactive status
- Last modified date and author
From this screen, you can also click Edit to open the trigger in a basic editor, or click the Active checkbox to toggle it on or off directly.
Note: Deactivating a trigger from this screen is available in sandbox environments.
Trigger Best Practices Every Developer Should Follow
One Trigger Per Object
Running multiple triggers on the same object creates unpredictable execution order. Salesforce does not guarantee which trigger fires first if you have two triggers on the same object for the same event.
Consolidate all logic for an object into a single trigger and route it through a handler class.
Always Bulkify Your Trigger
Salesforce processes records in batches of up to 200 records at a time. If your trigger uses a SOQL query inside a loop, you’ll hit governor limits almost immediately on any bulk operation.
// BAD — SOQL inside loop
for (Contact c : Trigger.new) {
Account a = [SELECT Id FROM Account WHERE Id = :c.AccountId]; // hits limit fast
}
// GOOD — query outside loop
Map<Id, Account> accountMap = new Map<Id, Account>(
[SELECT Id FROM Account WHERE Id IN :accountIds]
);
Always Write Test Classes
Salesforce requires 75% code coverage for production deployment, but that’s the floor — not the goal. Aim for 90%+ with meaningful assertions, not just coverage lines.
Research from Salesforce’s ecosystem shows that organizations with strong Apex test suites spend 60% less time on emergency bug fixes compared to orgs with minimal coverage.
Avoid Hardcoding IDs
Never hardcode Record Type IDs, Profile IDs, or any other org-specific IDs in your trigger. These IDs differ between sandbox and production, which will break your deployment.
Use dynamic queries or Custom Labels instead.
Common Trigger Activation Errors and How to Fix Them
Error: “Compile Error: Unexpected token”
This is a syntax error. Double-check your Apex for missing semicolons, unclosed brackets, or incorrect method signatures. The Developer Console highlights the line number — go straight to it.
Error: “System.LimitException: Too many SOQL queries: 101”
You’ve hit the SOQL governor limit. The fix: move your queries outside of loops. Use collections (Lists, Maps, Sets) to batch your queries.
Error: “Average test coverage across all Apex Classes and Triggers must be at least 75%”
Your test class coverage isn’t meeting the production deployment threshold. Write additional test methods that cover uncovered code paths — especially else branches and exception handlers.
Error: “Apex trigger [TriggerName] caused an unexpected exception”
This usually means a null pointer exception inside your trigger logic. Add null checks before accessing related fields:
if (c.AccountId != null) {
// safe to proceed
}
Trigger Context Variables You Need to Know
Salesforce makes several context variables available inside every trigger. These are critical for writing conditional logic:
Variable | What It Tells You |
Trigger.new | List of new record versions (insert/update) |
Trigger.old | List of old record versions (update/delete) |
Trigger.newMap | Map of ID to new record |
Trigger.oldMap | Map of ID to old record |
Trigger.isBefore | True if before event |
Trigger.isAfter | True if after event |
Trigger.isInsert | True if insert event |
Trigger.isUpdate | True if update event |
Trigger.isDelete | True if delete event |
Trigger.size | Total number of records in trigger |
Using Trigger.new vs Trigger.newMap depends on your use case. If you need to look up records by ID, Trigger.newMap is faster. If you’re iterating through all records, Trigger.new is your default.
Deploying Triggers to Production
There are three primary ways to deploy an Apex trigger from sandbox to production:
Change Sets — The traditional admin-friendly method. Navigate to Setup → Change Sets → Outbound Change Sets, add your trigger and test class, and deploy.
Salesforce CLI (sf / sfdx) — The developer-preferred method. Enables version-controlled deployments via CI/CD pipelines.
sf project deploy start –source-dir force-app/main/default/triggers
Metadata API — Used for automated deployments from tools like Jenkins, GitHub Actions, or Copado.
Regardless of method, every deployment to production runs all Apex tests in the org. Your coverage must be at or above 75% for the deployment to succeed.
According to a 2023 Salesforce developer survey, over 68% of deployment failures are caused by insufficient test coverage or tests that rely on hardcoded data — both preventable with proper development habits.
Conclusion
Activating a trigger in Salesforce is straightforward once you understand the fundamentals — but doing it well requires following the right patterns from day one.
The key takeaways:
- Triggers activate automatically when saved; control them with Custom Setting toggles
- Always use the one-trigger-per-object pattern with a dedicated handler class
- Bulkify everything — Salesforce processes records in batches, and governor limits are unforgiving
- Deploy with proper test coverage (aim above 90%) to avoid production failures
- Use context variables precisely to build conditional logic that handles every scenario
Mastering Salesforce trigger architecture puts you in control of your automation stack. And when your systems run cleanly, your team can focus on what actually moves the needle — building pipeline and closing deals.
🚀 Skip Manual Setups. Book More Meetings.
We handle targeting, campaign design, and scaling so your pipeline never stalls.
7-day Free Trial |No Credit Card Needed.
FAQs
How do I activate a trigger in Salesforce for the first time?
Can I use Salesforce triggers to automatically generate leads from new records?
What is the difference between a before and after trigger in Salesforce?
How many triggers can you have per object in Salesforce?
We deliver 100–400+ qualified appointments in a year through tailored omnichannel strategies
- blog
- Sales Development
- How to Activate a Trigger in Salesforce