Sub topic 3
What Salesforce Can Do on Its Own
This chapter assumes you know Salesforce objects and records, and that you can read REST and JSON. It surveys the integration tools built into the platform. You need them in mind before you can judge when they are enough.
The native toolkit
Salesforce has five main integration tools. Each fits a different direction and pattern.
| Tool | Direction | Typical pattern | Code needed |
|---|---|---|---|
| Flow (HTTP callout action, external services) | Outbound | Request-reply, fire-and-forget | Low or none |
| Apex callouts | Outbound | Request-reply | Yes |
| Named Credentials | Outbound (authentication) | Supports all callouts | None |
| Platform Events | Both | Publish-subscribe | Optional |
| REST, SOAP, Bulk, and Pub/Sub APIs | Inbound | Request-reply, batch sync, streaming | In the caller |
Named Credentials
A Named Credential stores an endpoint URL and its authentication settings in one place. Your code refers to it by name, so it never contains secrets. Salesforce adds the token or password when the request goes out. Changing the endpoint between sandbox and production then becomes a configuration change.
Apex callouts
An Apex callout is an HTTP request made from code. For one simple request-reply integration, it is often all you need. The callout: prefix tells Salesforce to resolve the endpoint through a Named Credential.
String orderId = '10045';
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'));
}A common error is making a callout after uncommitted DML in the same transaction. Salesforce rejects it with an "uncommitted work pending" exception. Make the callout first, or move it into asynchronous Apex such as a Queueable class.
Flow
Flow can call external REST APIs without code. You can use an HTTP Callout action, or import an OpenAPI schema as an External Service and call its operations as Flow actions. A record-triggered flow can run when an order changes. This suits admins who own the integration and an API that is simple and well documented.
Platform Events
A Platform Event is a message published to Salesforce's event bus. The publisher does not know who subscribes. Subscribers can be Apex triggers, flows, or external clients using the Pub/Sub API.
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());
}
}Check isSuccess(). publish does not throw an exception when it fails. Events are also kept for a limited time, so a subscriber that is down for too long loses them.
Inbound APIs
External systems can call Salesforce through the REST API for single records and small sets, and through the Bulk API for large loads. You can also expose custom endpoints with @RestResource Apex classes. Authentication is usually OAuth through a Connected App or External Client App.
What to notice
Every tool above connects Salesforce to something else. None of them sits between two systems that are not Salesforce. Transformation, retries, and monitoring are left to you. Each is possible in Apex, but you write and maintain that code yourself.
Exercise
A flow updates an Order and then, in the same Apex transaction, a callout to the ERP fails with "uncommitted work pending". What caused it, and what are two fixes?
Show answer
The callout ran after DML that had not yet committed, which Salesforce does not allow. Fix it by making the callout before the DML, or by moving it into asynchronous Apex such as a Queueable. Publishing a Platform Event and letting a separate subscriber make the callout also works.