Every org has a story, and it usually begins the same way: one Apex trigger here, another there, each solving a problem in the moment. But over time, orgs quietly accumulate multiple Apex triggers on the same object, written by different developers with no unified strategy. Before long, you’re staring down unpredictable execution orders, governor limit landmines, logic conflicts, and recursion nightmares. Sound familiar?

Of course, managed package triggers are a black box. We don’t own them, and we can’t touch them. But our custom triggers? That’s our house, and we get to set the rules. The answer isn’t more triggers. It’s one, done right.

That’s where Agentforce Vibes becomes your best pair programmer. It reads through your triggers, tells you exactly what each one is doing, spots the landmines, and helps you consolidate everything into a clean, scalable pattern iejggcbhdbklunjvfhejciflcuchnvrn the way it should have been built from the start. What would typically take days of manual code review and architectural planning gets done in a fraction of the time, so your team can focus on what actually matters building great solutions.

In my org, I had multiple triggers firing on the Opportunity object, each one written at a different point in time, by different developers, for different reasons. In this post, I’ll walk you through the approach I used to bring all of that chaos under one roof, into a single, clean trigger while intentionally leaving any managed package triggers untouched.

The workflows, skills, prompts, and steps you’ll see throughout this post are based on my own org, and your org will certainly look different. Your trigger count, logic complexity, and dependency map are uniquely yours. That’s completely normal, and honestly, that’s the point.

From concept to practice: Skills and workflows

What makes this approach powerful is that you can turn it into a reusable skill or workflow — a framework that you can run on any object, share across your team, or plug into other agents. The principles are universal; the execution is yours to shape.

Skills: Your on-demand code analyst

Think of a skill as a specialist you can call on instantly. Point it at your triggers and it surfaces what actually matters: recursion traps, SOQL inside loops, conflicting field assignments, or gaps in test coverage. Analysis that used to take hours of careful reading? It now takes minutes.

Workflows: Safe, repeatable refactoring at scale

A workflow takes that analysis and turns it into a structured, sequenced process: audit first, then refactor what needs fixing, then generate the consolidated trigger, handler, and tests in the right order. No steps skipped, no code merged before it’s ready. Just a clean, predictable path from messy to maintainable.

From chaos to a single, clean trigger

Below is the a generic workflow that I implemented to take multiple Apex triggers on a single object and consolidate them into one clean, maintainable handler.

Step Description
1. Workflow start You run the /trigger-consolidation command to start the trigger consolidation workflow.
2. Metadata retrieval The trigger consolidation workflow uses the Salesforce CLI to query the org’s metadata and pull all trigger and class sources.
3. Trigger analysis The workflow runs the 15-dimension risk assessment on your code thanks to the two trigger analysis skills.
4. Plan creation The workflow writes the audit plan to docs/trigger-audit-plan.md.
5. Plan review You read, edit, adjust, and approve the plan.
6. Plan execution The workflow reads your plan, and generates files that implement the trigger consolidation.
7. Manual review You review generated files and resolve the // CONFLICT and // ASYNC REQUIRED comments.

This workflow works on any standard or custom object as it contains no hardcoded object names. You can run it multiple times on different objects. You’ll get the same process and the same quality of analysis without the need for reconfiguration.

Let’s take a look at the workflow steps in more detail.

Step 0: Project setup

Before you get started, there are a few things you’ll need in place.

This walkthrough assumes that you’re working in VS Code with the Salesforce Extension Pack installed, your org is authenticated via the Salesforce CLI, and the Salesforce DX MCP Server is enabled in Agentforce Vibes. If you’re already set up, feel free to skip ahead. Otherwise, each link above will get you there quickly.

I’ve published a base version of the skills and workflows for trigger consolidation on GitHub. Think of it as your ready-to-run starting point: fork it, adapt it to your org’s objects and conventions, and make it your own.

Now, let’s install the skills and workflows. Head over to this README for setup instructions, it walks you through copying the .a4drules/ directory into your Salesforce DX project root.

This copies three files into your project: apex-trigger-risk-scan.md, pex-trigger-consolidation-analysis.md, and trigger-consolidation.md.

That’s the entire install — no restart or configuration required. Agentforce Vibes picks up the new skills and workflow immediately.

Step 1: Workflow start

  1. Click the Agentforce Vibes icon in the VS Code Activity Bar to open the chat panel.
  2. Type the following command to start the trigger consolidation workflow.
  3. The workflow kicks off by asking which object to work on.

  4. Type an object name like Opportunity and press Enter.

Step 2: Metadata retrieval

The workflow uses the Salesforce CLI to query your org’s metadata to retrieve Apex triggers and the related helper classes.

You can watch it work in the chat panel with an output similar to this:

Step 3: Trigger analysis

With the source in hand, the workflow loads the two trigger analysis skills and runs every trigger through all risk dimensions.

Per-trigger scan: apex-trigger-risk-scan skill (seven dimensions)

Dimension What It Catches
Execution order risk Flags assumptions that the trigger makes about running before or after another specific trigger, e.g., comments or field reads that depend on values another trigger should set first.
Recursion traps Flags absence of a static Boolean recursion guard when DML is present — CRITICAL if no guard and DML exists, and HIGH if no guard only.
Governor limit exposure Scans for SOQL inside loops (CRITICAL), DML inside loops (CRITICAL), synchronous HTTP callouts in trigger context (HIGH), and aggregate queries without LIMIT (MEDIUM).
Before/after context boundary Maps which contexts that the trigger fires in and flags misplaced logic, e.g., field assignments in after context (silently lost) or DML on triggering record in before context (causes recursion).
Bypass mechanism Looks for kill-switch patterns, such as custom setting/metadata checks, permission-based skips, or static Boolean flags set externally, and notes absence as well as presence.
Static variable inventory Lists every static variable in handler/helper classes and flags generic names (isRunning, processed) that collide with the same name in another trigger’s class after consolidation.

Cross-trigger analysis: apex-trigger-consolidation-analysis skills (eight dimensions)

Dimension What It Checks
Logic overlap Compares all triggers and flags the same field assigned in multiple triggers, identical validation logic, and the same helper method called from multiple triggers.
Conflicting logic Flags contradictions across triggers on the same event, such as opposing field values, one trigger nulling a field another reads, mutually exclusive conditionals, or read-after-write dependencies.
Helper class coupling Maps helper/utility classes to dependent triggers, i.e., any class used by more than two triggers is a consolidation risk since modifying it can break all callers.
Test coverage gaps Determines per trigger whether or not a test class exists, coverage is below 85%, or there’s an absence of a bulk test with 200+ records.
Static variable collision Cross-references static variable inventories from all per-trigger scans and identifies name collisions, such as two classes with the same static variable name, corrupting recursion guards.
Bypass consolidation strategy Assesses whether bypass mechanisms can be unified: compatible bypasses merge into one; inconsistent bypasses require per-method flags; or triggers with no bypass that inherit a unified bypass unintentionally are flagged.
Automation re-entry risk Identifies flows, process builders, or workflow field updates that can commit DML re-firing Apex triggers mid-transaction, and flags @future/Queueable/Platform Event calls that could loop back.
Cumulative governor limit budget Sums SOQL, DML, and heap consumption across all triggers to surface compound risk invisible per-trigger, and flags when the combined estimate approaches Salesforce limits even if no single trigger is problematic alone.

You’ll see findings stream into the panel in real time.

Step 4: Plan creation

Here’s the part that matters: the workflow does not generate a single line of code yet.
Instead, it writes a detailed plan file to docs/trigger-audit-plan.md and pauses.

Step 5: Plan review

Open the plan file in VS Code. It contains:

  • Trigger inventory: Every trigger, its events, line count, risk score, and top finding
  • Risk register: Every finding with severity, plain-English description, and recommendation
  • Dependency graph: Which triggers share which helper classes, as a plain-text diagram
  • Logic merge map: Exactly which logic from which trigger goes into each consolidated context, with conflicts flagged explicitly
  • Best practices compliance: Where current triggers violate Apex standards and what the consolidated code will fix

This file is yours to edit. You can change the instructions, add team notes, remove a trigger from the plan, etc. When you’re ready to proceed, the workflow reads your edited version before it writes any code. What you see in the plan is what gets built.

When the plan looks right, go back to the Agentforce Vibes chat panel and type:

Step 6: Plan execution

The workflow reads your (possibly edited) plan and generates consolidated files directly into your Salesforce DX project.

For instance, this is what you could see when consolidating an opportunity trigger:

In this example, these four files are generated:

  • OpportunityTrigger.trigger: This is a single trigger covering only the events that existed across the originals. No business logic in the body, everything delegates to the handler.
  • OpportunityTriggerHandler.cls: One method per trigger context has a static Boolean recursion guard at the top, and every method is annotated with its consolidated source triggers. Logic is merged in the correct sequence per the Logic Merge Map you reviewed.
  • OpportunityTriggerHelper.cls: This is the actual business logic, fully ported from the original triggers. All SOQL is bulkified outside loops, and duplicate logic from multiple triggers appears exactly once.
  • OpportunityTriggerTest.cls: Test methods for single insert, bulk insert (200 records), update, bulk update, and delete are each traceable back to the source trigger.

Step 7: Manual review

Once the workflow terminates, the last manual task is to review the comments that were left during the execution. There are two types of comments that you should address:

  • // CONFLICT: Where two triggers set the same field to different values. You decide which wins
  • // ASYNC REQUIRED: Where a sync callout needs to move to @future or Queueable before deploying

Finally, the trigger jungle is a solved problem. Once a trigger is optimized, you can leverage prompts to deactivate the old trigger and, once tested and validated, promote the new implementation to a higher environment.

Considerations

  • Before you adapt this framework, make it yours. The workflow and skill are starting points, not scripts. Your org’s complexity, naming conventions, and business logic are unique to everything generated here, so edit the audit plan before you commit to it.
  • Spend real time in plan mode before generating any code. Open docs/trigger-audit-plan.md, work through the phases, flag the conflicts you already know about, and align the merge order with your team’s plan. The more thinking you do upfront, the cleaner the output.
  • Treat generated code as a recommendation, not a final answer. Always review and validate before you deploy. These are smart suggestions, not certified production-ready commits.
  • Fix High severity issues before you consolidate anything. If the audit surfaces recursion risks, SOQL or DML in loops, or field conflicts, stop and resolve those first. Merging broken logic into a single trigger doesn’t fix it and just makes it harder to debug later.
  • Version control everything. This includes triggers, handler classes, service classes, audit files, test classes — all of it. If it gets generated or modified, it belongs in source control.
  • Always test in a sandbox first and never run directly against production. The workflow connects to whichever org you’re authenticated to, so before running /trigger-consolidation, verify that sf org list shows a sandbox or scratch org as your default. Consolidating triggers in production without testing is how outages happen.
  • Run one object at a time, but think about the whole org. This isn’t a one-and-done fix. Once Opportunity is clean, move to Case, then Account, then your custom objects. The phased approach is intentional: each consolidation builds confidence for the next.

Conclusion

Trigger sprawl is common and costly, and with the right approach, it’s completely solvable.

Ready to clean up your org? Start with your simplest triggers, follow the steps in this guide, build confidence, then tackle the high-traffic objects. Run the Agentforce Vibes analysis skill and let the audit canvas show you exactly what you’re dealing with. One trigger, one handler, zero surprises.

Resources

Agentforce Vibes

Salesforce CLI

Apex Trigger Best Practices

Metadata & Deployment

About the author

Lakshmi Anusha Myneni is a Principal Technical Architect at Salesforce with 15 years of experience in the financial services industry. She partners closely with customers during the most critical stages of their journey, translating complex business challenges into compelling, technically sound Salesforce solutions.