Sub topic 2
Request-Reply, Fire-and-Forget, and Batch Sync
Assume you know Salesforce objects, basic Apex, REST, JSON, HTTP methods, and status codes. These patterns differ mainly in when the sender expects work to finish and how it handles failure.
Request-reply: calling out and waiting for an answer
Request-reply is synchronous. Salesforce sends a request and waits for the external system's response before continuing. Use it when the caller needs an immediate result, such as checking an invoice status.
A Named Credential stores the endpoint and authentication configuration. The Apex code therefore avoids hard-coded hosts and secrets.
public with sharing class InvoiceClient {
public static String fetchInvoiceStatus(String invoiceId) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Billing_API/invoices/' +
EncodingUtil.urlEncode(invoiceId, 'UTF-8'));
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setTimeout(10000);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() != 200) {
throw new CalloutException('Billing API returned ' +
res.getStatusCode());
}
Map<String, Object> body =
(Map<String, Object>) JSON.deserializeUntyped(res.getBody());
return (String) body.get('status');
}
}A timeout or non-success status must become a controlled error. Do not retry blindly inside a user transaction.
Fire-and-forget: publishing events without waiting
Fire-and-forget is asynchronous. Salesforce publishes a platform event and continues without waiting for subscribers to finish. This reduces coupling, but subscribers process the event later and can fail independently.
Order_Shipped__e eventMessage = new Order_Shipped__e(
Order_Number__c = '00123',
Carrier__c = 'ACME'
);
Database.SaveResult result = EventBus.publish(eventMessage);
if (!result.isSuccess()) {
for (Database.Error error : result.getErrors()) {
System.debug(LoggingLevel.ERROR, error.getMessage());
}
}Batch sync: moving large volumes with Bulk API 2.0
Batch synchronization moves many records in a job rather than one request at a time. Bulk API 2.0 is appropriate for scheduled imports, exports, and large backlogs.
POST /services/data/v60.0/jobs/ingest
Content-Type: application/json
{
"object": "Account",
"externalIdFieldName": "External_Id__c",
"contentType": "CSV",
"operation": "upsert",
"lineEnding": "LF"
}Upload CSV data to the job, then mark the job UploadComplete. An upsert uses the external ID to update an existing record or create a new one, making repeated batches safer.
Exercise
Which pattern fits a nightly load of two million ERP records into Salesforce?
Show answer
Batch sync with Bulk API 2.0 and an external ID upsert.