Sub topic 9
SOAP API and when it still fits
This chapter assumes you know HTTP and JSON basics, understand Salesforce objects and records, and have read the earlier chapters on authentication and the REST API. SOAP is older than REST in Salesforce, but it is still supported and still the right choice in a few situations.
What the SOAP API is
The SOAP API is a Salesforce API that exchanges XML messages over HTTPS. Its contract is a WSDL (Web Services Description Language) file, an XML document that lists every operation, object, and field type a client can use. Tools read the WSDL and generate client code, so you call typed methods instead of building XML by hand.
Salesforce publishes two WSDLs:
| WSDL | Typing | Fits |
|---|---|---|
| Enterprise | Strongly typed to your org's objects and fields | A single org whose schema rarely changes |
| Partner | Loosely typed, generic sObjects with name/value fields | Tools that must work across many orgs |
The Enterprise WSDL is generated for your org. If an admin adds or removes a field, you may need to regenerate it and rebuild the client. The Partner WSDL does not depend on one org's schema, so it survives those changes.
Core operations
SOAP exposes explicit operations, not HTTP verbs on resource URLs:
login()authenticates with a username and password (plus security token). Salesforce is retiring this call in newer API versions, so prefer an OAuth access token as the session ID.query()andqueryMore()run SOQL and page through results.create(),update(),upsert(), anddelete()change records, up to 200 records per call.describeSObject()returns field and object metadata.
Every call goes to one endpoint, such as /services/Soap/c/60.0, and carries a SessionHeader with the session ID.
A sample request
This request runs a query using an OAuth access token as the session ID. It uses the Enterprise SOAP namespace.
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 envelope holding a queryResponse with the records and a done flag.
When SOAP still fits
Choose SOAP when one of these applies:
- Existing SOAP tooling. Legacy middleware, ESBs, and enterprise systems often speak only SOAP. Rewriting them has a cost that SOAP avoids.
- Strict contracts. A WSDL gives compile-time checking of types, which some teams want in Java or .NET.
- Operations REST lacks in the same form. Some utility calls, such as
getDeleted()andgetUpdated()for replicating changes, are available in SOAP. - Partner-built tools. Tools that work across many orgs use the Partner WSDL.
For new integrations without those constraints, REST is usually simpler. It uses JSON, needs no generated code, and has more resources, such as Composite.
Common errors
- INVALIDSESSIONID: the token is expired or sent to the wrong instance. Refresh the token and check the host.
- Stale Enterprise WSDL: a field or object changed and the generated client fails. Regenerate the WSDL, or move to the Partner WSDL.
- Wrong namespace: using the Partner namespace with an Enterprise request produces a fault. Match
urn:enterprise.soap.sforce.comorurn:partner.soap.sforce.comto the WSDL you use. - Batch size over 200:
create()andupdate()reject larger arrays. Chunk the records, or use Bulk API 2.0 for large volumes.
Exercise
Your team maintains a Java application that integrates with 40 customer orgs, each with different custom fields. Which WSDL fits, and why?
Show answer
The Partner WSDL. It is loosely typed and does not depend on one org's schema, so one client works across all 40 orgs. The Enterprise WSDL is generated for a single org and would need regeneration for each one.
Exercise
A SOAP create() call fails when you send 500 Contact records. What are two ways to fix it?
Show answer
Split the records into batches of 200 or fewer and send multiple calls, or move the load to Bulk API 2.0, which is built for large volumes.