Most CRM orgs that have been customized for a decade have a trigger nobody wants to touch. It calculates something the business cares about, and the person who wrote it moved on long ago. Sooner or later someone proposes rewriting all of it as a proper service.
The goal is usually right. Doing it all at once is the wrong way to get there.
I spent years building on Salesforce at LinkedIn, and the bias I came away with is that the customizations are the business. Most of the odd validation rules exist because somebody once had a good reason, and the reason rarely made it into a document. A big-bang rewrite asks you to rediscover every one of those reasons at once, on a deadline.
What I’d do instead is the strangler pattern. You put a thin layer in front of the old logic, build the new version next to it, and move one capability at a time until the old code has nothing left to do. Martin Fowler named it in 2004 after the strangler fig, which grows around a host tree until it can stand on its own.
The screens stay where they are
Users keep working in the CRM screens they already know. On save, the code that used to do the work first asks a router which implementation owns this capability. The router reads a flag with three possible values. old runs the logic that’s already in the CRM. new hands the work to the new service, which writes its answer back to the record. shadow runs both, keeps the old answer, and records whether the two agreed.
On Salesforce I’d make the router a small Apex class that reads its flags from custom metadata, so an admin can flip one without a code release. The hand-off to the service has to be asynchronous there, because a trigger can’t make a synchronous callout.
Whichever path runs, the result lands back on the CRM record as ordinary fields. People underrate this part. Reports, list views, approval processes, and that integration another team built off those fields years ago all keep working, because from where they sit nothing moved. Users see one system, and nobody has to learn a new screen.
I have two strong opinions about the router. Keep it dumb. It reads a flag and dispatches, and the day it grows a business rule you’ve built a third system you’ll have to migrate later. And put the flag on the capability. “Discount approval level” gets a flag. “The pricing service” doesn’t, because one service will end up owning several capabilities and you’ll want to move them in different weeks.
Shadow mode does most of the work
Shadow mode is where you learn what the old code really does. The spec and the code disagree more often than anyone in the planning meeting expects.
Mismatches tend to land in four buckets. Bugs in the new service are the easy kind. Then there are bugs in the old logic that the business has quietly adapted to, differences nobody cares about (rounding in the fourth decimal place), and timing: the old trigger saw the record before a flow changed it, and the new service saw it after.
I’d push back hard on the instinct to fix the old bugs during the migration. Reproduce them. Cut over with the same behavior, then fix the bug as its own change behind its own flag. If you fix and migrate at once and a number moves, you won’t know which change moved it.
Discount approvals, one region at a time
Suppose quote discount approvals live in the CRM. A trigger works out an approval level from the discount percentage and the deal size, with different thresholds per region, and an approval process routes the quote to a manager or a VP based on a field called Approval_Level__c.
The team builds a pricing service that computes the same level, and the trigger starts asking the router first. The flag begins at old, moves to shadow, and for a few weeks every quote save sends a snapshot plus the old answer to the service, which computes its own and logs any difference.
Cutover can be narrow, with one region on new while everyone else stays in shadow:
# The router reads this and nothing else.
# Shown as YAML; on Salesforce these would be custom metadata records.
capabilities:
discount_approval_level:
mode: shadow # old | shadow | new
new_for_regions: [ANZ] # these regions already get the new answer
renewal_quote_defaults:
mode: new
lead_assignment:
mode: old
For ANZ quotes the service writes Approval_Level__c back to the record, the existing approval process reads it exactly as before, and the sales rep never learns anything changed. If the numbers look wrong, rollback is one line in that file.
The compare is about twenty lines
I’d run the compare inside the new service rather than in the CRM. CRM platforms limit how much work a single save can do, and the shadow call shouldn’t block the user anyway. On Salesforce the shadow request goes out through a Queueable job or a platform event, like any other hand-off, and the service does the comparing.
import json
import logging
log = logging.getLogger("shadow")
def shadow_compare(capability, record_id, old_result, new_fn, payload, ignore=()):
"""Run the new implementation beside the old one and log any difference.
In shadow mode the caller always gets the old result back."""
try:
new_result = new_fn(payload)
except Exception as exc:
log.warning(json.dumps({"capability": capability, "record_id": record_id,
"outcome": "new_failed", "error": repr(exc)}, default=str))
return old_result
diffs = {
key: {"old": old_result.get(key), "new": new_result.get(key)}
for key in sorted(set(old_result) | set(new_result))
if key not in ignore and old_result.get(key) != new_result.get(key)
}
log.info(json.dumps({"capability": capability, "record_id": record_id,
"outcome": "mismatch" if diffs else "match",
"diffs": diffs}, default=str))
return old_result
Fill in ignore before the first shadow run. Timestamps and generated IDs will never match, and if you don’t exclude them the mismatch log fills with noise and people stop reading it.
A checklist for each capability
I’d copy this into the ticket for each capability. The blanks are on purpose, because the right bar differs for every one.
Capability: ____________________ Business owner: ____________
Before shadow
[ ] Describe the capability in one sentence. If it needs "and", split it.
[ ] Find every entry point: triggers, flows, plugins, buttons,
scheduled jobs, inbound integrations.
[ ] Write its inputs and outputs as CRM fields, not as code.
[ ] Decide which fields the new service writes back, and who owns each.
[ ] Add the flag with values old, shadow, new. Default: old.
[ ] New service is idempotent on record ID plus a version or
last-modified stamp.
[ ] Shadow calls are bulk-safe: one async request per trigger batch,
not one per record.
During shadow
[ ] Users still see the old result.
[ ] Every mismatch is logged with record ID and both values.
[ ] Mismatches reviewed weekly and labeled: new bug, old bug,
timing, or accepted difference.
[ ] Shadow has run through month end (and quarter end if it matters).
[ ] Mismatch rate under ______ for ______ days in a row.
Cutover
[ ] Switch to new for one segment first: a region, team, or record type.
[ ] Someone has flipped the flag back to old once, on purpose.
[ ] Write-back runs as an integration user, and the router skips
this capability for saves by that user.
[ ] Reports, list views, and approval processes read the same fields.
[ ] Support knows the date and what changed.
After
[ ] Old logic off but still deployed for one full cycle.
[ ] Old code, old automation, and the flag itself deleted.
Write-back and the mismatch bar
Write-back is the weakest joint. If the service writes asynchronously, the record shows the previous value for a moment. For a field a rep reads right after clicking Save, that matters, and the screen has to call the service before the save, which is more work than it sounds. In the example, a quote submitted for approval inside that window would route on a stale level, so I’d make submission wait for the write-back.
Write-back also wakes up the CRM’s own automation. Have the service write as a dedicated integration user and have the router skip this capability when that user saves, or the two systems will keep nudging each other. Skip only this capability, though. Other automation on the record may need to see the new value.
The mismatch bar has to be set per capability. Zero sounds right and is rarely achievable for anything involving dates or rounding. Agree on each number with the business owner in the room, rather than picking one figure for the whole program.
The last box on the checklist matters most. As long as the old trigger is still deployed, somebody will eventually fix a bug in it.