Sub topic 5
API versions and keeping metadata compatible
This chapter assumes you can read a Salesforce DX project, you know what a metadata file is, and you have deployed code or flows to a sandbox. You should also know how the release timeline works from earlier chapters.
What an API version is
Every Salesforce release adds one to the API version number. Winter '26, for example, is version 65.0. A version fixes the behavior of the platform APIs and of the metadata that uses them. Salesforce keeps old versions working so existing code does not break each time a release arrives.
An API version appears in three places:
- Metadata files. Apex classes, triggers, Visualforce pages, Lightning web components, and flows each store the version they were saved with.
- Project configuration.
sourceApiVersioninsfdx-project.jsonsets the version used when you deploy or retrieve source. - Integration calls. External systems call versioned endpoints such as
/services/data/v65.0/.
Each of these can sit at a different version, and each can be raised separately.
Why old versions matter
An Apex class saved at version 50.0 keeps running with the behavior of version 50.0, even after the org moves to a later release. That stability is the point of versioning. It also lets technical debt build up. The older the version, the more likely it is that:
- you cannot use newer features in that component,
- Salesforce eventually retires the version, and calls to it fail,
- a later bulk upgrade changes behavior across many components at once.
Salesforce announces retirements of the oldest API versions in the release notes. Read those notes for every release.
Nothing above should be drawn as a diagram. Use the diagram below instead.
Setting the project version
Set the project version in sfdx-project.json:
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"sourceApiVersion": "65.0"
}Raise this value on purpose, after you have tested against the new release. Do not raise it automatically on release day. The version in each component's meta file is separate. An Apex class has a file like this:
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<status>Active</status>
</ApexClass>Finding old versions
To find out which components lag behind, list the versions stored in your source:
grep -rho "<apiVersion>[0-9.]*</apiVersion>" force-app \
--include="*-meta.xml" | sort | uniq -c | sort -rnThe output shows how many components use each version. Treat anything far behind the current version as a candidate for review. Also check your integrations. Look at the connected apps, middleware, and scripts that call the REST or SOAP APIs, and note the version in each endpoint URL.
Raising versions safely
Raise versions in small groups, and test each group:
- Pick a group of components, such as one package or one feature area.
- Raise the
apiVersionin a preview sandbox. - Run the local Apex tests and review the results.
- Exercise the related flows and user journeys by hand or with UI tests.
- Deploy to production in a normal release window.
Keep each change in its own commit. If a regression appears, you can then revert one group without touching the others.
Common errors
| Symptom | Likely cause |
|---|---|
| Deploy fails with an unsupported version message | `sourceApiVersion` is higher than the target org supports, for example a non-preview sandbox while the project uses the preview version |
| Integration suddenly returns errors | The endpoint version has been retired |
| A component behaves differently after a version change | A behavior change between versions that tests did not cover |
Exercise
Your project's sourceApiVersion is 65.0, but you must deploy to a sandbox that has not yet received the new release. What problem can occur, and how do you avoid it?
Show answer
The deployment can fail because the target org does not support the newer API version. Avoid it by using a sourceApiVersion the target org supports, or by deploying to a preview sandbox. Raise the version only once the target environment is on the matching release.