Sub topic 3
What Salesforce can do on its own: Flow, Apex callouts, Named Credentials, Platform Events, and APIs
This chapter assumes you know Salesforce objects and records, can read a REST call and a JSON body, and have a rough idea of what an integration between two systems involves. It covers the tools that ship with the platform. The later chapters use this inventory to decide when those tools are enough and when middleware earns its place.
Two directions of traffic
Every native option moves data in one of two directions.
- Outbound: Salesforce calls or notifies another system.
- Inbound: Another system calls Salesforce, or listens to it.
Keeping the direction in mind makes the list below easier to sort.
Flow
Flow is the declarative automation tool. It can reach outside the org in three ways.
- HTTP Callout action. You define the external endpoint, request, and response shape in a point-and-click editor, and Flow calls it. This suits simple request-reply calls that an admin can own.
- External Services. You register an OpenAPI specification. Salesforce generates invocable actions from its operations, and Flow uses them like any other action.
- Platform Event-triggered flows. A flow starts when an event message is published. This is the declarative subscriber.
Flow also publishes events, through a Create Records element on a platform event object. A callout from a record-triggered flow is only allowed on the asynchronous path, because a synchronous before-save or after-save path cannot wait on an external system.
Apex callouts
Apex gives full control over the HTTP request: headers, retries, parsing, and branching on status codes. A callout is an HttpRequest sent with the Http class. This example reads one order from an ERP.
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 code assumes orderId is already defined. Note the endpoint. It starts with callout: and names a Named Credential, not a hard-coded URL.
Callout rules that cause the most errors
- A callout cannot run after uncommitted DML in the same transaction. Salesforce raises an "uncommitted work pending" error. Make the callout first, or move it to an asynchronous context.
- Triggers cannot make synchronous callouts. Use
@future(callout=true), Queueable Apex withDatabase.AllowsCallouts, or a platform event. - Each callout has a timeout. The default is 10 seconds and the maximum is 120 seconds.
- The number of callouts per transaction is capped, and the combined timeout of all callouts is also capped.
Check the current Apex limits documentation for exact numbers, because they change between releases.
Named Credentials
A Named Credential stores the endpoint URL and the authentication details in one place that Flow and Apex can both refer to. Your code never sees the secret.
The current model splits the pieces.
- A Named Credential holds the URL and points to an External Credential.
- An External Credential holds the authentication protocol, such as OAuth 2.0 or a custom header, and its principals.
- A permission set grants users or automation access to the External Credential principal.
This has practical effects. Tokens refresh without code. Moving from sandbox to production means changing the credential, not the code. A secret never appears in source control. The permission set also means a user who lacks access gets a callout failure even when the Apex is correct, which is a common first-time surprise.
Platform Events
A platform event is a message that Salesforce publishes to an event bus. Publishers do not know who subscribes. This is the native publish-subscribe option.
You define the event as a custom object with the __e suffix, then publish it 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());
}
}A subscriber inside Salesforce can be an Apex trigger on the event object.
trigger OrderEventSubscriber on Order_Event__e (after insert) {
for (Order_Event__e e : Trigger.new) {
System.debug('Order ' + e.Order_Number__c + ' is ' + e.Status__c);
}
}External subscribers connect through the Pub/Sub API, which uses gRPC, or through the older CometD streaming interface. Each message carries a replay ID. A subscriber that disconnects can resume from its last replay ID, within the retention window. For high-volume events that window is 72 hours.
Three properties matter for design.
- Delivery is at-least-once. A subscriber can see the same message more than once, so it must tolerate duplicates.
- Publishing is not tied to the transaction by default. Depending on the event's publish behavior setting, the event can be published immediately or only after the transaction commits. Choose "publish after commit" when subscribers must not see events from a transaction that rolls back.
- Allocations apply. Publishing and delivery volumes are limited per edition.
Change Data Capture
Change Data Capture (CDC) publishes an event whenever a record is created, updated, deleted, or undeleted on selected objects. You do not write the publisher. It suits keeping an external system in step with Salesforce data.
Salesforce APIs for inbound traffic
When another system drives the conversation, it calls one of the platform APIs.
| API | Typical use | Notes |
|---|---|---|
| REST API | Create, read, update, and delete records from web and mobile clients | JSON, one record or small sets |
| Composite API | Several dependent operations in one request | Reduces round trips |
| SOAP API | Older enterprise clients, strongly typed contracts | WSDL based |
| Bulk API 2.0 | Loading or extracting large data sets | Asynchronous jobs, CSV |
| GraphQL API | Reading related records in one query | Client chooses fields |
| Pub/Sub API | Subscribing to platform events and CDC | gRPC streaming |
You can also expose your own endpoint with an @RestResource Apex class. This lets an external system call business logic instead of raw records. You then own the versioning, error format, and test coverage of that endpoint.
Authentication for inbound calls normally uses an OAuth flow through a connected app or external client app. The calling system gets a token and presents it with each request.
Legacy options you may still meet
Outbound Messages, defined on legacy workflow rules, send a SOAP message to an endpoint when a record changes. They still run in many orgs. For new work, prefer a platform event, CDC, or a Flow callout.
Choosing within the native set
| Need | Usual native choice |
|---|---|
| Admin-maintained call to a simple REST service | Flow HTTP Callout |
| Call with custom parsing, retry, or branching logic | Apex callout |
| Authenticate to an external service | Named Credential |
| Tell unknown listeners that something happened | Platform Event |
| Mirror record changes outward | Change Data Capture |
| External system loads or reads records | REST or Bulk API 2.0 |
These tools cover many single-system, request-reply, and event-notification integrations without extra infrastructure. What they do not give you is central transformation, cross-system orchestration, or shared monitoring. Later chapters take up those gaps.
Exercise
A trigger on Order__c must send the order to an ERP over REST, and the ERP needs an OAuth 2.0 token. You write the Apex callout directly in the trigger and hard-code the token in the Apex class. List three problems with this design and give the native fix for each.
Show answer
First, triggers cannot make synchronous callouts, so move the call to Queueable Apex that implements Database.AllowsCallouts, or publish a platform event and let a subscriber make the call. Second, a hard-coded token is a security risk and will expire, so use a Named Credential with an External Credential that handles OAuth and refreshes the token. Third, the endpoint is hard-coded and differs between sandbox and production, so use the callout: prefix with the Named Credential name, and change the credential per environment instead of changing code.
Exercise
An external billing system subscribes to a platform event and receives the same message twice after a reconnect. Why can this happen, and what must the subscriber do?
Show answer
Platform event delivery is at-least-once, and a subscriber that resumes from a replay ID can receive a message again. The subscriber must be idempotent. For example, it can record the event's identifier or the business key (such as the order number) and skip work it has already done.