Sub topic 2
Reducing Round Trips with Salesforce API Resources
Assume you know HTTP, JSON, Salesforce records, and basic SOQL. A round trip is one request from a client to Salesforce and its response. Fewer round trips usually mean lower latency and less client-side coordination.
Composite and Batch Resources
The Batch REST resource sends up to 25 independent REST requests in one HTTP request. Salesforce returns each result separately. Use it when requests do not depend on one another.
The Composite resource also combines requests, but later requests can use values from earlier responses. With allOrNone: true, Salesforce rolls back the transaction if one subrequest fails.
{
"allOrNone": true,
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Account",
"referenceId": "account",
"body": { "Name": "Acme Corp" }
},
{
"method": "POST",
"url": "/services/data/v60.0/sobjects/Contact",
"referenceId": "contact",
"body": {
"LastName": "Smith",
"AccountId": "@{account.id}"
}
}
]
}API Limits and Error Handling
Salesforce applies org-level API limits and per-request limits. Grouping requests reduces network overhead, but it does not make unlimited work possible. Inspect HTTP status codes and every subresponse. A successful outer Composite response can contain a failed inner request.
Bulk API 2.0 for Large Data Volumes
Use Bulk API 2.0 for large asynchronous imports or exports. Create an ingest job, upload CSV data, and mark the upload complete. Salesforce processes the job later, so it fits high volume better than thousands of synchronous REST calls. Poll the job, then retrieve failed-record results.
Apex REST for Custom Logic
Apex REST exposes server-side Apex code at a custom URL. Use it when standard resources cannot express a transaction or business rule. Validate input, enforce sharing, and return useful HTTP errors.
@RestResource(urlMapping='/AccountSummary/*')
global with sharing class AccountSummaryResource {
@HttpGet
global static Account getAccount() {
String id = RestContext.request.requestURI.substringAfterLast('/');
return [SELECT Id, Name, Industry FROM Account WHERE Id = :id];
}
}SOAP API When It Still Fits
SOAP remains useful for existing enterprise integrations, generated strongly typed clients, and contracts based on WSDL. Choose REST for most new record integrations unless a required system or governance standard already depends on SOAP.
Exercise
Which resource fits two independent updates, and which fits creating a Contact that needs a newly created Account ID?
Show answer
Use Batch for the independent updates and Composite for the dependent Account-and-Contact operation.