Sub topic 3
The Salesforce integration toolbox
This chapter maps the main Salesforce integration surfaces to the three patterns. It assumes you know the Salesforce data model, basic Apex, REST and JSON, and HTTP methods and status codes. Authentication is covered in the next chapter, and each pattern gets its own chapter after that.
Direction and pattern are separate questions
Two questions decide which feature you reach for.
- Direction. Is Salesforce the caller (outbound) or the receiver (inbound)?
- Pattern. Does the caller wait for an answer (request-reply), hand off a message (fire-and-forget), or move many records at once (batch sync)?
A single feature can serve more than one pattern. The REST API, for example, can be used for request-reply or as a building block in a batch design. The table below gives the usual fit.
| Surface | Direction | Usual pattern | Notes |
|---|---|---|---|
| Apex callouts | Outbound | Request-reply | Your code sends an HTTP request and waits for the response. |
| REST API | Inbound | Request-reply | An external system reads or writes records and gets an immediate result. |
| Platform events | Both | Fire-and-forget | Custom messages published to an event bus. Subscribers process them later. |
| Change Data Capture (CDC) | Outbound | Fire-and-forget | Salesforce publishes change events when records are created, updated, deleted, or undeleted. |
| Bulk API 2.0 | Inbound | Batch sync | Asynchronous jobs that load or query large volumes of records. |
Apex callouts
An Apex callout is an HTTP request that your code sends to an external system. Apex waits for the response before the next line runs. This is the standard way to implement outbound request-reply. Use it when a user or process needs the external answer right now, such as checking an invoice status.
Callouts are subject to governor limits, covered in a later chapter. They also cannot run in the middle of an uncommitted database transaction, so plan where they sit in your code.
REST API
The REST API lets an external system work with Salesforce records over HTTP and JSON. Each call returns a status code and a body, so the caller knows the result straight away. It works well for interactive, low-volume operations.
Addressing a record by an external ID makes the call an upsert: it creates the record if no match exists and updates it otherwise.
PATCH /services/data/v60.0/sobjects/Account/External_Id__c/ERP-10042
Content-Type: application/json
Authorization: Bearer <access_token>
{
"Name": "Acme Corporation",
"Industry": "Manufacturing"
}Sending this request twice does not create a duplicate. A later chapter on idempotency builds on this behavior.
Platform events
A platform event is a custom message that you define, much like a custom object with a __e suffix. A publisher sends it to the event bus, and any number of subscribers receive it. Subscribers can be Apex triggers, flows, or external clients.
The publisher does not wait for subscribers. It only learns whether Salesforce accepted the event.
Order_Shipped__e evt = new Order_Shipped__e(
Order_Number__c = '00123',
Carrier__c = 'ACME'
);
Database.SaveResult result = EventBus.publish(evt);
if (!result.isSuccess()) {
for (Database.Error err : result.getErrors()) {
System.debug(LoggingLevel.ERROR, 'Publish failed: ' + err.getMessage());
}
}Order_Shipped__e is an example event that you would define in your org first.
Change Data Capture
Change Data Capture publishes a change event whenever a tracked record changes. You do not write publishing code. You select the objects to track, and subscribers receive the changes. It suits keeping an external system up to date with Salesforce data without polling.
Use a platform event when you want to announce a business fact you define, such as "order shipped". Use CDC when you want a stream of data changes.
Bulk API 2.0
Bulk API 2.0 handles large data loads and queries asynchronously. You create a job, upload CSV data, and close the job so Salesforce processes it in the background. You then check the job status and download the results.
This request creates an upsert job that matches Accounts on an external ID field:
POST /services/data/v60.0/jobs/ingest
Content-Type: application/json
Authorization: Bearer <access_token>
{
"object": "Account",
"externalIdFieldName": "External_Id__c",
"contentType": "CSV",
"operation": "upsert",
"lineEnding": "LF"
}The next steps are to upload the CSV data to the job's batches endpoint and then set the job state to UploadComplete. A later chapter walks through the full job.
How the surfaces connect
Common mistakes
- Using Apex callouts for large data movement. Callouts have per-transaction limits, and Bulk API 2.0 is built for volume.
- Using platform events when the caller needs an immediate answer. The publisher gets no result from subscribers.
- Using CDC when you need a custom business message. CDC reports field changes, not business meaning.
- Polling the REST API for changes that CDC would deliver for you.
Exercise
An ERP system must load 2 million product records into Salesforce every night, matching on an external ID. Which surface fits, and which pattern is it?
Show answer
Bulk API 2.0 with an upsert job on the external ID field. This is the batch sync pattern, because the volume is large and the load can run asynchronously without a user waiting for the result.
Exercise
A warehouse system should learn that an order shipped, but the Salesforce process that records the shipment must not wait for the warehouse. Which feature fits, and what does the publisher find out?
Show answer
A platform event (or a similar event-based feature) fits, which is fire-and-forget. The publisher only learns whether Salesforce accepted the event. It does not learn whether any subscriber processed it.