Sub topic 2
Common integration patterns and where Salesforce fits in each
This chapter assumes you know basic Salesforce concepts (objects, records, APIs), understand REST and JSON, and have a general idea of what an integration between two systems is. It names four patterns and shows where Salesforce plays a part in each. Later chapters use these patterns to decide between native tools and middleware.
Why patterns matter
An integration pattern describes how two systems exchange data. It covers who starts the exchange, whether the sender waits, and how much data moves at once. Choosing the pattern comes before choosing a tool. A mismatch between the pattern and the tool is a common cause of fragile integrations.
| Pattern | Who waits? | Typical volume | Salesforce example |
|---|---|---|---|
| Request-reply | Caller waits for the answer | One record at a time | Show live order status on a page |
| Fire-and-forget | Caller does not wait | One record at a time | Send a new order to a fulfillment system |
| Batch synchronization | Nobody waits | Thousands to millions of records | Nightly load of accounts from an ERP |
| Publish-subscribe | Publisher does not know subscribers | Events as they happen | Announce that an order shipped |
Request-reply
In request-reply, one system sends a request and waits for a response before it continues. It fits cases where the user needs the answer now, such as looking up an order's status in the ERP while viewing an Account.
Salesforce is usually the caller. An Apex callout through a Named Credential is the simplest native form.
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'));
}The main risk is coupling in time. If the ERP is slow or down, the user waits or sees an error.
Fire-and-forget
In fire-and-forget, the sender hands off a message and moves on. It does not wait for the work to finish. Salesforce can send the message, or it can receive one through the REST API. The sender gets, at most, an acknowledgment that the message was accepted.
This pattern protects user experience because a slow target does not block the save. The cost is that failures happen later and out of sight. You need a plan for retries and for alerting someone. Later chapters cover that plan.
Batch synchronization
Batch synchronization moves large sets of records on a schedule. Typical cases are initial data loads and nightly reconciliations. Here Salesforce is often the target. An external job calls the Bulk API to insert or upsert records. Salesforce can also run scheduled Apex to pull or push data, but the size of each job is limited by governor limits.
A key design choice is the external ID. A field marked as an external ID lets an upsert match records from the other system without looking up Salesforce IDs first.
Publish-subscribe
In publish-subscribe, a publisher emits an event and any number of subscribers react. The publisher does not know who listens. Salesforce supports this with Platform Events.
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());
}
}A middleware flow, a Flow, or an Apex trigger can subscribe to this event. Adding a new subscriber later requires no change to the publisher. That is the main benefit over direct calls.
How the patterns relate
Common errors
- Using request-reply for work that takes many seconds, which leaves users waiting and risks timeouts.
- Treating fire-and-forget as guaranteed delivery without any retry or error record.
- Running a large batch through record-by-record callouts instead of the Bulk API.
- Publishing events without deciding who is responsible for failed subscribers.
Exercise
A sales rep opens an Account and needs the customer's current credit balance from an external finance system, shown on the page within a second or two. Which pattern fits, and what is the main risk?
Show answer
Request-reply fits, because the user needs the answer immediately. The main risk is time coupling: if the finance system is slow or unavailable, the page is slow or shows an error, so you need timeouts and a fallback message.
Exercise
A nightly job must load 2 million product records from an ERP into Salesforce, matching existing records by the ERP's product code. Which pattern and which Salesforce features fit?
Show answer
Batch synchronization. Use the Bulk API with an upsert on an external ID field that stores the ERP product code, so no Salesforce ID lookup is needed.