Sub topic 14
Anti-patterns to avoid
This chapter assumes you know Salesforce native integration options (Flow, Apex callouts, Named Credentials, Platform Events), the main governor limits, and what a middleware layer does. An anti-pattern is a design that looks reasonable but causes predictable problems. Some of these come from using too little middleware and some from using too much.
Anti-patterns from using too little middleware
Point-to-point sprawl
Each new system gets its own Apex callout or Flow connection to Salesforce and to every other system. Five systems can need ten connections, and each one has its own mapping, credentials, and error handling. A change in one system's API breaks several integrations.
Callouts inside loops or triggers
A callout per record runs into governor limits (100 callouts per transaction). It also holds the user's transaction open while it waits for a slow external system.
// Anti-pattern: one callout per record, inside a trigger loop
for (Order__c o : Trigger.new) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:ERP_Credential/orders/' + o.Id);
req.setMethod('POST');
new Http().send(req); // fails after 100 callouts and blocks the save
}A better design sends one batched request from asynchronous code, or publishes a Platform Event and lets a subscriber do the work.
public class OrderSync implements Queueable, Database.AllowsCallouts {
private List<Id> orderIds;
public OrderSync(List<Id> orderIds) { this.orderIds = orderIds; }
public void execute(QueueableContext ctx) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:ERP_Credential/orders/batch');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody(JSON.serialize(orderIds));
HttpResponse res = new Http().send(req);
if (res.getStatusCode() >= 300) {
System.debug('Sync failed: ' + res.getStatus());
}
}
}No retries or idempotency
A callout that fails is logged and forgotten, or it is retried without a unique key. The result is lost updates or duplicate orders. Every write across systems needs an idempotency key, such as an external ID, and a defined retry policy.
Credentials and mappings buried in code
Hardcoded endpoints, tokens, and field mappings in Apex are hard to rotate and audit. Use Named Credentials for authentication at minimum.
Anti-patterns from using too much middleware
Middleware for one simple request
If one Salesforce Flow or Apex callout reaches one stable REST endpoint, a middleware layer adds licensing cost, a new deployment pipeline, and another thing to monitor with no benefit.
Business logic in the wrong layer
Pricing rules or approval logic placed in middleware flows are invisible to Salesforce admins. Keep logic about Salesforce data in Salesforce. Keep routing, transformation, and orchestration in middleware.
Middleware as a database
Storing state long term, or running heavy reporting through the integration layer, gives you a system nobody owns. Persist business data in a system of record.
Long synchronous chains
A user clicks a button, and Salesforce calls a process API that calls three system APIs in sequence. Latency adds up, and one slow system times out the whole request. Use asynchronous patterns, such as events, wherever the user does not need the answer immediately.
Anti-patterns in operation
- No monitoring or alerting. Failures are found by users, not by dashboards.
- No owner. Nobody is named for the integration after go-live.
- Polling when events exist. Frequent queries for changes burn API limits. Use Platform Events or Change Data Capture.
Exercises
Exercise
A trigger on Order__c makes one HTTP callout per record to an ERP. Users report that bulk loads fail. Name the problem and two ways to fix it.
Show answer
The trigger makes callouts inside a loop, which exceeds the 100-callout governor limit and holds the transaction open. Fixes: send one batched request from a Queueable that implements Database.AllowsCallouts, or publish a Platform Event and let a subscriber, such as a middleware flow, do the work.
Exercise
A team plans to buy MuleSoft to connect Salesforce to a single, stable REST API for read-only order status. Which anti-pattern is this, and what would you recommend?
Show answer
Middleware for one simple request. Recommend an Apex callout through a Named Credential, or an External Services action in Flow, until more systems, transformations, or reliability needs justify middleware.