Sub topic 9
SOAP API and when it still fits
This chapter assumes you know HTTP and JSON basics, and that you can read a SOQL query. It also assumes you have seen the REST API and OAuth 2.0 from the earlier chapters. Here you will learn what the SOAP API is, how it differs from REST, how to call it, and when it is still the right choice.
What the SOAP API is
The SOAP API is Salesforce's original web service interface. SOAP stands for Simple Object Access Protocol. Clients send XML messages over HTTPS to a single endpoint. The structure of those messages is described by a WSDL (Web Services Description Language) file, which is a machine-readable contract listing every operation, object and field type.
Salesforce publishes two WSDLs:
| WSDL | Typing | What it contains | Typical use |
|---|---|---|---|
| Enterprise WSDL | Strongly typed | Your org's objects and fields, including custom ones | Single-org tools where the schema rarely changes |
| Partner WSDL | Loosely typed | Generic `sObject` with name-value fields | Tools that must work with many orgs, such as ISV products |
You download either one from Setup. The Enterprise WSDL is tied to one org's schema. If an admin adds a custom field, you regenerate the WSDL and rebuild the client to see it as a typed property. The Partner WSDL does not change when the schema changes, so one client works across orgs, but you handle field names and types yourself.
How a SOAP call works
Every call is an HTTP POST of an XML envelope. The envelope has an optional header, which carries the session ID and options, and a body, which carries the operation and its arguments.
Authentication
The SOAP API accepts a session ID in a SessionHeader. You can get one in two ways:
- OAuth 2.0. Obtain an access token as shown in the authentication chapter and pass it as the session ID. This is the recommended path.
- The
login()call. Send a username and password (plus security token) to the SOAP endpoint. Salesforce is retiring SOAPlogin()in newer API versions, so do not build new integrations on it. Check the current release notes for the cutoff for your org.
An OAuth token works with SOAP because the token is the session. Using it keeps credentials out of your code and lets you use the same connected app as your REST clients.
A query call by hand
The following example sends a query request with curl. It assumes $ACCESS_TOKEN holds an OAuth token and that your My Domain is MyDomain. The Enterprise endpoint path ends with the API version.
curl -X POST "https://MyDomain.my.salesforce.com/services/Soap/c/60.0" \
-H "Content-Type: text/xml; charset=UTF-8" \
-H "SOAPAction: query" \
-d '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:enterprise.soap.sforce.com">
<soapenv:Header>
<urn:SessionHeader>
<urn:sessionId>'"$ACCESS_TOKEN"'</urn:sessionId>
</urn:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<urn:query>
<urn:queryString>SELECT Id, Name FROM Account LIMIT 5</urn:queryString>
</urn:query>
</soapenv:Body>
</soapenv:Envelope>'The response is an XML document with a queryResponse element. It holds a done flag, a size, the records, and a queryLocator when more rows remain. In practice most teams do not write envelopes by hand. They generate client stubs from the WSDL with a tool such as Java's wsimport or .NET's svcutil, then call methods like connection.query(soql).
Core operations
SOAP exposes a richer set of record operations than the basic REST resources. These are the ones you will meet most often:
| Operation | Purpose | Notes |
|---|---|---|
| `query`, `queryMore`, `queryAll` | Run SOQL and page through results; `queryAll` includes deleted rows | Default batch size is 500 records and can be set up to 2,000 |
| `search` | Run SOSL | Returns matches across several objects |
| `create`, `update`, `upsert`, `delete` | Write up to 200 records per call | Each record gets its own `SaveResult` |
| `retrieve` | Fetch records by Id with chosen fields | No SOQL needed |
| `describeSObject`, `describeGlobal` | Read object metadata | Useful for schema discovery |
| `getUpdated`, `getDeleted` | List record Ids changed in a time window | Used for polling-based sync |
| `convertLead`, `merge`, `undelete` | Perform record operations that have no simple CRUD form | Part of the original SOAP surface |
Partial success and `allOrNone`
A DML call (a create, update or delete) with several records returns one result per record. By default, some records can succeed while others fail. You must read every result, because the HTTP status will still be 200. To make the whole call fail if one record fails, send the AllOrNoneHeader with allOrNone set to true. This is the same idea as allOrNone in the Composite example from the earlier chapter.
Useful headers
SOAP uses headers for call options. A few are worth knowing:
QueryOptionssets the query batch size.AssignmentRuleHeadermakes a case or lead go through assignment rules.DuplicateRuleHeadercontrols duplicate rule behavior on insert and update.CallOptionsidentifies your client application, which helps in debugging with Salesforce support.EmailHeadercontrols whether notification emails go out.
SOAP versus REST
Both APIs reach the same data and obey the same sharing rules and field-level security for the authenticated user. Both count against the same daily API request allocation. The differences are in format and ergonomics.
| Aspect | SOAP API | REST API |
|---|---|---|
| Payload | XML in an envelope | JSON (or XML) |
| Contract | WSDL, generates typed client code | Documented resources, OpenAPI available |
| Endpoint | One URL, many operations | Many URLs, HTTP verbs |
| Payload size | Larger because of XML | Smaller |
| Errors | SOAP fault or per-record result | HTTP status plus a JSON error array |
| Tooling fit | Strong in Java, .NET, enterprise middleware | Strong everywhere, especially web and mobile |
Salesforce does not deprecate SOAP API as a whole, but its newest capabilities usually arrive in REST first. Treat SOAP as stable, supported and mature rather than as the place where new features land.
When SOAP still fits
Pick SOAP when a concrete constraint points to it. Do not pick it out of habit. These are the cases where it is a sound choice.
- Existing tools speak SOAP. Many enterprise middleware products, ETL tools and older integrations have built-in SOAP connectors. Rewriting them for REST adds cost and risk with no gain.
- You need a strict contract. A WSDL gives compile-time checking. In a Java or .NET shop, a generated client catches a misspelled field name at build time, not at runtime.
- You use a SOAP-only operation. The operation list above includes calls that have less direct support in REST. If you need an operation only SOAP offers, use it.
- A multi-org product needs the Partner WSDL. A single generic client that handles any org's schema is a practical fit for ISVs.
- You are calling out from Apex. Apex can consume an external WSDL by generating classes from it. That is the reverse direction (Salesforce calling another system), but it is the same technology and a frequent reason to learn it.
Choose REST instead when you write a new web or mobile client, want smaller payloads, or are working in a language with weak SOAP tooling, such as most JavaScript environments. Choose Bulk API 2.0 when you move large volumes of records, because SOAP writes cap at 200 records per call and each call uses one API request.
Common errors and gotchas
INVALID_SESSION_ID. The token expired or belongs to a different org or domain. Refresh the token and send the call to theserverUrlor My Domain for that org.- Stale Enterprise WSDL. A new field does not appear in your client, or a deleted field causes a fault. Regenerate the WSDL and rebuild.
- Ignoring per-record results. An HTTP 200 does not mean every record saved. Loop through every
SaveResultand checksuccess. - Mixing up endpoints. Enterprise uses
/services/Soap/c/{version}and Partner uses/services/Soap/u/{version}. Using the wrong namespace for the chosen endpoint causes a fault. - Chatty loops. Updating records one at a time burns your daily request limit. Batch up to 200 per call, or move to Bulk API 2.0.
- Hard-coded API versions. Pin a version deliberately and plan upgrades. Old versions are retired on a published schedule.
Summary
The SOAP API is a stable, contract-driven interface that uses XML and a WSDL. It reaches the same data as REST and shares its limits. It earns its place where existing tooling, strict typing or multi-org clients call for it. For new, lightweight clients, REST is usually simpler.
Exercise
Your team runs a Java integration that must keep working across many customer orgs, each with different custom fields. Which WSDL fits, and why? Also, a nightly job updates 50,000 contacts through SOAP in 200-record calls. What would you change?
Show answer
Use the Partner WSDL. It is loosely typed and does not depend on any one org's schema, so one client works against every org, and you read custom fields by name. For the nightly job, 50,000 records at 200 per call needs 250 API requests, and you must check every SaveResult. Moving to Bulk API 2.0 handles large volumes asynchronously with far fewer requests, so that is the better choice.
Exercise
A SOAP create call with 10 records returns HTTP 200, yet 3 records are missing in Salesforce. What happened, and how can you make the call all-or-nothing?
Show answer
By default, SOAP DML allows partial success. The HTTP 200 only means the call itself was processed, and the 3 failed records have SaveResult entries with success set to false and error details. To roll back every record when any one fails, send the AllOrNoneHeader with allOrNone set to true.