Sub topic 2
Common integration patterns and where Salesforce fits in each
This chapter assumes you know basic Salesforce concepts (objects, records, and the platform APIs), that you understand REST and JSON, and that you have a general idea of what an integration between two systems is. It builds on the integration styles from the previous chapter. Styles describe how systems are wired together. Patterns describe how a single interaction behaves over time.
There are four patterns to know: request-reply, fire-and-forget, batch synchronization, and publish-subscribe. For each one, this chapter shows what Salesforce does natively, where it runs into trouble, and what that means for the middleware decision later in the tutorial.
Why patterns come before tools
A pattern is a reusable shape for one conversation between systems. Choosing the pattern first matters because the same two systems can need different patterns for different data. A support agent who looks up an order status needs an immediate answer. A nightly price list refresh does not. Choosing a tool before the pattern often leads to a design that is too heavy for one case and too weak for the other.
Ask these questions about each interaction:
- Does the caller need an answer before it can continue?
- How many records move at once?
- How many systems care about the result?
- What should happen if the other side is down?
The answers point to one of the four patterns.
The flowchart is a simplification. Real interactions often combine patterns, for example a publish-subscribe event that triggers a request-reply lookup. It is still a useful first filter.
Request-reply
In request-reply, the caller sends a request and waits for a response. The caller's next step depends on that response. Typical examples are checking inventory when a user opens a quote, validating an address, or fetching an order status from an ERP system (enterprise resource planning, the system of record for orders and finance).
Where Salesforce fits
Salesforce acts as the caller in two common ways:
- Apex callouts. Apex code sends an HTTP request to an external endpoint and reads the response.
- External Services and Flow. A Flow calls an external REST API that has been described with an OpenAPI specification, without code.
Salesforce can also be the target. An external system calls the Salesforce REST API or an Apex REST class and waits for the reply.
Here is a native callout through a Named Credential. A Named Credential stores the endpoint and authentication details outside the code.
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:ERP_Credential/orders/' + orderId);
req.setMethod('GET');
HttpResponse res = new Http().send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> order = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
System.debug(order.get('status'));
}Edge cases
- Latency is felt by the user. If the ERP takes eight seconds, the Salesforce page waits eight seconds. Synchronous callouts have a timeout, with a maximum of 120 seconds per callout.
- Callouts cannot follow uncommitted work. Apex does not allow a callout after a DML statement in the same transaction without special handling. Do the callout first, or move it to asynchronous code such as Queueable Apex.
- Callouts from triggers must be asynchronous. A trigger cannot make a synchronous callout, so a trigger that needs one hands the work to a Queueable or a future method.
- Many calls to many systems. One request-reply to one system is simple. A request that must call three systems, combine their answers, and handle a failure in the second call is orchestration. Apex can do it, but the logic becomes code you must test, version, and maintain.
Fire-and-forget
In fire-and-forget, the sender passes a message on and continues without waiting for the business result. The sender may get an acknowledgment that the message was accepted, but not the outcome. Examples are sending a closed-won opportunity to a billing system, or logging an audit entry.
Where Salesforce fits
Salesforce supports this in several native ways:
- Outbound Messages from workflow-style automation send a SOAP message to an endpoint and retry it for up to 24 hours. Salesforce has been moving new work toward Flow, so check current guidance before choosing this for new builds.
- Flow with an HTTP callout action sends a request after a record change.
- Queueable Apex with a callout runs after the transaction commits, so the user is not blocked.
- Platform Events publish a message that a subscriber processes later.
Edge cases
Fire-and-forget hides failure. If the receiver rejects the message, the user who saved the record has already moved on. You must decide in advance:
- Who is told about the failure?
- Is the message retried, and how many times?
- Can the same message be delivered twice without harm? This property is called idempotency, and a later chapter covers it in detail.
Native tools give you some of this. Outbound Messages retry automatically. A Queueable job that fails leaves no automatic retry unless you code one. Without a plan for these questions, records quietly drift out of sync.
Batch synchronization
In batch synchronization, many records move together on a schedule or in a one-time load. Examples are a nightly sync of product prices from an ERP, an initial migration of a million contacts, or a weekly export to a data warehouse.
Where Salesforce fits
- Bulk API 2.0 loads or extracts large volumes asynchronously. It is built for volume and is the usual choice for loads in the hundreds of thousands of records and above.
- Data Loader and Data Import Wizard support manual or scripted loads.
- Batch Apex processes large record sets in chunks inside Salesforce, and can make callouts if it implements
Database.AllowsCallouts. - Scheduled Flow runs a flow at a set time against a set of records.
- Change Data Capture can feed an outbound sync so the target receives only changed records, not a full extract.
Edge cases
- Full versus delta. A full sync moves everything each time. A delta sync moves only what changed. Delta syncs are cheaper but need a reliable way to detect change, such as a last-modified timestamp or Change Data Capture.
- Matching records. Loads that update existing records need an external ID field so that Salesforce can upsert (update if found, insert if not) without duplicates.
- Partial failure. In a batch of 10,000 rows, a few will fail. You need a way to find them, correct them, and resubmit only those.
- Limits. Daily API and Bulk API allocations apply. The next chapters on native options and limits cover them. A sync that works at 10,000 records may break at 10 million.
Publish-subscribe
In publish-subscribe, a publisher emits an event and does not know who receives it. Any number of subscribers react independently. This decouples the systems. Salesforce can add a new subscriber without touching the publisher.
Where Salesforce fits
Salesforce publishes and consumes several kinds of events:
- Platform Events are custom messages you define, for example
Order_Event__e. - Change Data Capture publishes an event whenever a record is created, updated, deleted, or undeleted.
- Pub/Sub API lets external clients subscribe to these events over gRPC, and also publish to Salesforce.
Here is a custom Platform Event published from Apex:
Order_Event__e evt = new Order_Event__e(Order_Number__c = '10045', Status__c = 'Shipped');
Database.SaveResult result = EventBus.publish(evt);
if (!result.isSuccess()) {
for (Database.Error err : result.getErrors()) {
System.debug(err.getMessage());
}
}The publisher does not know whether a middleware flow, a Salesforce trigger, or nobody is listening. That is the point of the pattern.
Edge cases
- Retention is limited. Platform Events are kept on the event bus for a limited window, 72 hours for high-volume events. A subscriber that is offline longer than that loses messages unless it can replay from a stored position or recover another way.
- Delivery is at least once in practice. Design subscribers to handle a repeated event.
- Publishing is not the same as delivery.
EventBus.publishreports whether the event was accepted onto the bus. It says nothing about whether a subscriber processed it. - Allocations apply. Event publishing and delivery have daily and hourly allocations that depend on edition and add-ons.
Comparing the patterns
| Pattern | Caller waits? | Typical volume | Native Salesforce tools | Main risk |
|---|---|---|---|---|
| Request-reply | Yes | One record at a time | Apex callout, External Services, Flow | Latency and timeouts block the user |
| Fire-and-forget | No | Low to moderate | Outbound Message, Flow, Queueable | Silent failure |
| Batch synchronization | No | Thousands to millions | Bulk API 2.0, Batch Apex, Scheduled Flow | Partial failure and API limits |
| Publish-subscribe | No | Moderate to high | Platform Events, Change Data Capture, Pub/Sub API | Retention window and duplicate delivery |
What the patterns reveal about middleware
Notice the pattern in the edge cases. For a simple, single-system interaction, the native tool is complete. The trouble starts when the interaction needs one of these:
- Several systems in one flow, so you need orchestration.
- Different data formats on each side, so you need transformation.
- Guaranteed delivery with retries and a place to inspect failures.
- Systems that speak protocols Salesforce does not, such as SFTP, JMS, or SOAP with specific security.
These are the functions of a middleware layer, covered in the chapters ahead. The patterns themselves do not require middleware. A direct Apex callout is a legitimate request-reply implementation. A Platform Event with an Apex trigger is a legitimate publish-subscribe implementation. The decision depends on how many of the conditions above apply and how costly it is to build them yourself.
Worked example: one business event, three patterns
A company closes an opportunity in Salesforce. Three things must happen:
- The sales rep's page shows live credit status from the finance system before approval. This is request-reply, because the rep waits for the answer.
- The closed deal is sent to the billing system. This is fire-and-forget, because the rep does not need billing's response to continue. A Queueable callout or Platform Event can carry it.
- Fulfillment, analytics, and a notification service all need to know about the sale. This is publish-subscribe, because Salesforce should not need to know how many systems care.
Separately, a nightly job refreshes product prices from the ERP. This is batch synchronization.
One Salesforce org, one event, four patterns. Each uses a different native tool, and each has different failure behavior. A single integration approach rarely suits all four, so the question for later chapters is which of them stay native and which move to middleware.
Exercise
A customer service agent clicks a button in Salesforce to see the shipping carrier's live tracking status. The carrier exposes one REST endpoint, and the agent waits on the page for the result. Name the pattern, name the native Salesforce tool you would try first, and name one edge case you must handle.
Show answer
The pattern is request-reply. Try an Apex callout through a Named Credential first, or a Flow with an External Services action if you want to avoid code. Edge cases include the carrier being slow, so you need a timeout and a friendly error message on the page, and the rule that a callout cannot be made after uncommitted DML in the same transaction. Because this is one call to one system with no transformation beyond simple JSON, native tools are likely enough.
Exercise
A subscriber system was offline for four days while Salesforce kept publishing high-volume Platform Events. What problem does this cause, and why does it matter for choosing between a simple subscriber and a middleware layer?
Show answer
Platform Events are retained on the event bus for only a limited window, 72 hours for high-volume events, so the subscriber can lose the events from the gap that falls outside that window. It must then recover the missed changes another way, such as a batch reconciliation. A middleware layer, or a durable queue it manages, can store messages while a target is down and replay them, which a bare subscriber cannot do on its own.