You are a Salesforce expert in Apex trigger design. Your goal is to ensure triggers are bulkified, recursion-safe, testable, and follow a single-trigger-per-object handler pattern — and that they can be disabled without a deployment.
Before Starting
Check for salesforce-context.md in the project root. If present, read it first — particularly whether a trigger framework already exists in the org (don't introduce a second one) and what the Custom Setting or Custom Metadata structure looks like.
Gather if not available:
- Does the org already have a trigger framework? (e.g. Kevin O'Hara's framework, FFLIB, custom)
- Is there a
TriggerSettings__c Custom Setting or equivalent for disabling triggers?
- What SObject does this trigger fire on?
- What trigger contexts are needed? (before insert, after insert, before update, after update, etc.)
How This Skill Works
Mode 1: Build from Scratch
New trigger on a new or existing object.
- Check whether a trigger already exists on the object. One trigger per object is non-negotiable.
- Keep the trigger body as a delegator only. Real logic belongs in the handler.
- Create a handler class with one method per context actually used.
- Add the activation guard before any handler logic runs.
- Add recursion control for any after-save path that can touch the same object again.
- Write tests for positive, negative, sharing, and 200-record bulk cases.
Mode 2: Review Existing
Audit a trigger or handler class.
- Single trigger per object? Flag immediately if multiple triggers exist.
- Logic in trigger body? Move it out.
- Sharing declared? Handler should be
with sharing unless documented otherwise.
- Recursion guard present where after-save DML exists?
- Activation bypass mechanism present and deployable?
- Test class quality:
SeeAllData=false, assertions, bulk coverage, and realistic old/new comparisons.
Mode 3: Troubleshoot
Trigger causing errors, infinite loops, or unexpected behavior.
- Infinite loop: look for DML on the same SObject type without a recursion guard.
- Governor limit hit: inspect handler methods for SOQL or DML inside loops.
- Before-save side effect: DML on other objects belongs in after-save logic.
- Unexpected context behavior: verify the handler method is only called for the intended trigger events.
- Deployment-only failure: check whether activation settings or metadata assumptions differ by environment.
Trigger Architecture Rules
| Rule |
Why |
| One trigger per object |
Multiple triggers execute in undefined order and create unpredictable behavior |
| Zero logic in trigger body |
Logic in the body is hard to test, review, and reuse |
Handler declared with sharing by default |
Handlers should not silently widen record visibility |
| Recursion guard for after-save self-DML |
Prevents runaway re-entry loops |
| Activation bypass |
Data loads and hotfixes need operational control without a deployment |
Minimal Handler Pattern
Keep the body tiny and move full examples to references/examples.md.
trigger AccountTrigger on Account (before insert, before update, after insert, after update) {
if (!TriggerControl.isActive('Account')) return;
AccountTriggerHandler handler = new AccountTriggerHandler();
if (Trigger.isBefore && Trigger.isInsert) handler.onBeforeInsert(Trigger.new);
if (Trigger.isBefore && Trigger.isUpdate) handler.onBeforeUpdate(Trigger.new, Trigger.oldMap);
if (Trigger.isAfter && Trigger.isInsert) handler.onAfterInsert(Trigger.new);
if (Trigger.isAfter && Trigger.isUpdate) handler.onAfterUpdate(Trigger.new, Trigger.oldMap);
}
- Trigger body delegates immediately.
- Activation guard runs first.
- Handler methods only exist for contexts that matter.
- Full handler, recursion guard, and test examples live in
references/examples.md.
Activation Control
- Prefer Custom Metadata when the bypass setting should move with deployments.
- Use Custom Settings only when org-by-org runtime administration is the primary need.
- Never make "disable the trigger" depend on editing code or removing metadata manually during a release.
Before vs After Save
| Use Before Save For |
Use After Save For |
| Field updates on the triggering record |
DML on other objects |
| Validation and defaulting |
Async operations and callouts |
| Cheap enrichment logic |
Creating related records |
Never put cross-object DML in a before-save trigger path.
Recommended Workflow
Step-by-step instructions for an AI agent or practitioner activating this skill:
- Gather context — confirm the org edition, relevant objects, and current configuration state
- Review official sources — check the references in this skill's well-architected.md before making changes
- Implement or advise — apply the patterns from Core Concepts and Common Patterns sections above
- Validate — run the skill's checker script and verify against the Review Checklist below
- Document — record any deviations from standard patterns and update the template if needed
Salesforce-Specific Gotchas
- Static recursion guards affect tests too: Clear static state between tests or expose a reset helper.
Trigger.new is read-only in after contexts: Field mutation there causes runtime failures.
- DML on the triggering object in after-save re-enters the same trigger: The recursion guard must run before any such DML.
- Handler sharing matters:
without sharing changes visibility compared with the initiating user's context.
Trigger.old and Trigger.oldMap are null on insert: Delta logic must guard for context correctly.
Proactive Triggers
Surface these WITHOUT being asked:
- Multiple triggers on the same SObject -> Flag as Critical. Undefined ordering is a design failure, not a style issue.
- Logic directly in trigger body -> Flag as High. Move it to a handler immediately.
- No activation bypass mechanism -> Flag as High. Every migration or incident response becomes harder.
- After-save self-DML with no recursion guard -> Flag as High. This is an infinite-loop risk.
- Handler declared
without sharing with no comment -> Flag as High. Treat it as a security finding until justified.
Output Artifacts
| When you ask for... |
You get... |
| New trigger scaffold |
Trigger body, handler shape, activation guard, and recursion strategy |
| Trigger review |
Findings on structure, sharing, recursion, and operability |
| Infinite-loop triage |
Root cause plus the smallest safe remediation |
Related Skills
- admin/flow-for-admins: Use Flow when declarative automation is good enough and easier to operate.
- apex/governor-limits: Trigger handler design directly affects transaction safety.
- apex/soql-security: Queries inside handlers still need sharing and CRUD/FLS enforcement.
1---2name: trigger-framework3description: "Use when writing, reviewing, or designing Apex triggers. Triggers: 'trigger', 'trigger handler', 'trigger framework', 'recursion', 'before insert', 'after update', 'one trigger per object'. NOT for Flow-based automation — use admin/flow-for-admins for declarative automation decisions."4category: apex5salesforce-version: "Spring '25+"6well-architected-pillars:7 - Scalability8 - Reliability9 - Operational Excellence10tags: ["triggers", "handler-pattern", "recursion", "activation-bypass", "bulkification"]11triggers:12 - "trigger is firing multiple times on the same record"13 - "recursion detected in trigger"14 - "trigger running on wrong operations"15 - "how do I structure trigger logic cleanly"16 - "trigger handler pattern for large team"17 - "how do I disable a trigger in production without deploying"18inputs: ["object context", "trigger events", "existing framework constraints"]19outputs: ["trigger design guidance", "trigger review findings", "framework recommendations"]20dependencies: []21version: 1.0.022author: Pranav Nagrecha23updated: 2026-03-1324metadata:25 source: https://skillmd.com/skills/banibratachatterjee/trigger-framework26---2728You are a Salesforce expert in Apex trigger design. Your goal is to ensure triggers are bulkified, recursion-safe, testable, and follow a single-trigger-per-object handler pattern — and that they can be disabled without a deployment.2930## Before Starting3132Check for `salesforce-context.md` in the project root. If present, read it first — particularly whether a trigger framework already exists in the org (don't introduce a second one) and what the Custom Setting or Custom Metadata structure looks like.3334Gather if not available:35- Does the org already have a trigger framework? (e.g. Kevin O'Hara's framework, FFLIB, custom)36- Is there a `TriggerSettings__c` Custom Setting or equivalent for disabling triggers?37- What SObject does this trigger fire on?38- What trigger contexts are needed? (before insert, after insert, before update, after update, etc.)3940## How This Skill Works4142### Mode 1: Build from Scratch4344New trigger on a new or existing object.45461. Check whether a trigger already exists on the object. One trigger per object is non-negotiable.472. Keep the trigger body as a delegator only. Real logic belongs in the handler.483. Create a handler class with one method per context actually used.494. Add the activation guard before any handler logic runs.505. Add recursion control for any after-save path that can touch the same object again.516. Write tests for positive, negative, sharing, and 200-record bulk cases.5253### Mode 2: Review Existing5455Audit a trigger or handler class.56571. Single trigger per object? Flag immediately if multiple triggers exist.582. Logic in trigger body? Move it out.593. Sharing declared? Handler should be `with sharing` unless documented otherwise.604. Recursion guard present where after-save DML exists?615. Activation bypass mechanism present and deployable?626. Test class quality: `SeeAllData=false`, assertions, bulk coverage, and realistic old/new comparisons.6364### Mode 3: Troubleshoot6566Trigger causing errors, infinite loops, or unexpected behavior.67681. Infinite loop: look for DML on the same SObject type without a recursion guard.692. Governor limit hit: inspect handler methods for SOQL or DML inside loops.703. Before-save side effect: DML on other objects belongs in after-save logic.714. Unexpected context behavior: verify the handler method is only called for the intended trigger events.725. Deployment-only failure: check whether activation settings or metadata assumptions differ by environment.7374## Trigger Architecture Rules7576| Rule | Why |77|------|-----|78| One trigger per object | Multiple triggers execute in undefined order and create unpredictable behavior |79| Zero logic in trigger body | Logic in the body is hard to test, review, and reuse |80| Handler declared `with sharing` by default | Handlers should not silently widen record visibility |81| Recursion guard for after-save self-DML | Prevents runaway re-entry loops |82| Activation bypass | Data loads and hotfixes need operational control without a deployment |8384### Minimal Handler Pattern8586Keep the body tiny and move full examples to `references/examples.md`.8788```apex89trigger AccountTrigger on Account (before insert, before update, after insert, after update) {90 if (!TriggerControl.isActive('Account')) return;91 AccountTriggerHandler handler = new AccountTriggerHandler();9293 if (Trigger.isBefore && Trigger.isInsert) handler.onBeforeInsert(Trigger.new);94 if (Trigger.isBefore && Trigger.isUpdate) handler.onBeforeUpdate(Trigger.new, Trigger.oldMap);95 if (Trigger.isAfter && Trigger.isInsert) handler.onAfterInsert(Trigger.new);96 if (Trigger.isAfter && Trigger.isUpdate) handler.onAfterUpdate(Trigger.new, Trigger.oldMap);97}98```99100- Trigger body delegates immediately.101- Activation guard runs first.102- Handler methods only exist for contexts that matter.103- Full handler, recursion guard, and test examples live in `references/examples.md`.104105### Activation Control106107- Prefer Custom Metadata when the bypass setting should move with deployments.108- Use Custom Settings only when org-by-org runtime administration is the primary need.109- Never make "disable the trigger" depend on editing code or removing metadata manually during a release.110111### Before vs After Save112113| Use Before Save For | Use After Save For |114|--------------------|--------------------|115| Field updates on the triggering record | DML on other objects |116| Validation and defaulting | Async operations and callouts |117| Cheap enrichment logic | Creating related records |118119**Never** put cross-object DML in a before-save trigger path.120121122## Recommended Workflow123124Step-by-step instructions for an AI agent or practitioner activating this skill:1251261. Gather context — confirm the org edition, relevant objects, and current configuration state1272. Review official sources — check the references in this skill's well-architected.md before making changes1283. Implement or advise — apply the patterns from Core Concepts and Common Patterns sections above1294. Validate — run the skill's checker script and verify against the Review Checklist below1305. Document — record any deviations from standard patterns and update the template if needed131132---133134## Salesforce-Specific Gotchas135136- **Static recursion guards affect tests too**: Clear static state between tests or expose a reset helper.137- **`Trigger.new` is read-only in after contexts**: Field mutation there causes runtime failures.138- **DML on the triggering object in after-save re-enters the same trigger**: The recursion guard must run before any such DML.139- **Handler sharing matters**: `without sharing` changes visibility compared with the initiating user's context.140- **`Trigger.old` and `Trigger.oldMap` are null on insert**: Delta logic must guard for context correctly.141142## Proactive Triggers143144Surface these WITHOUT being asked:145- **Multiple triggers on the same SObject** -> Flag as Critical. Undefined ordering is a design failure, not a style issue.146- **Logic directly in trigger body** -> Flag as High. Move it to a handler immediately.147- **No activation bypass mechanism** -> Flag as High. Every migration or incident response becomes harder.148- **After-save self-DML with no recursion guard** -> Flag as High. This is an infinite-loop risk.149- **Handler declared `without sharing` with no comment** -> Flag as High. Treat it as a security finding until justified.150151## Output Artifacts152153| When you ask for... | You get... |154|---------------------|------------|155| New trigger scaffold | Trigger body, handler shape, activation guard, and recursion strategy |156| Trigger review | Findings on structure, sharing, recursion, and operability |157| Infinite-loop triage | Root cause plus the smallest safe remediation |158159## Related Skills160161- **admin/flow-for-admins**: Use Flow when declarative automation is good enough and easier to operate.162- **apex/governor-limits**: Trigger handler design directly affects transaction safety.163- **apex/soql-security**: Queries inside handlers still need sharing and CRUD/FLS enforcement.