Sub topic 5
API Versions and Keeping Metadata Compatible
This chapter assumes you know basic Salesforce administration, have used sandboxes, and can run simple commands in a terminal. Each Salesforce release adds a new API version. Knowing which version your code and metadata use helps you upgrade on purpose and not by accident.
What an API version is
An API version is a number that identifies the set of behaviors, fields, and objects that Salesforce exposes at a given release. Each seasonal release increments it by one. Version 65.0, for example, belongs to the Winter '26 release. Spring '26 is 66.0 and Summer '26 is 67.0. Check the release notes or the developer documentation for the exact number of any release.
Salesforce keeps older versions working so that existing integrations and code do not break when the platform upgrades. When your org is upgraded, a component pinned to version 58.0 still behaves as it did under 58.0, in most respects. That pinning is what makes upgrades safe. It also means your org contains a mix of versions, and you have to manage that mix.
Where API versions appear
Many kinds of components carry their own version.
| Component | Where the version is set |
|---|---|
| Apex classes and triggers | The `-meta.xml` file next to each class, as `apiVersion` |
| Lightning Web Components | The `.js-meta.xml` file |
| Aura components | The `.cmp-meta.xml` file |
| Visualforce pages | The `.page-meta.xml` file |
| Flows | The `apiVersion` field of the flow metadata |
| Project-wide deploys | `sourceApiVersion` in `sfdx-project.json` |
| External integrations | The version number in the REST or SOAP endpoint URL |
An Apex class file and its metadata file look 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>Integrations use the version in the URL. A call to /services/data/v65.0/sobjects/Account uses version 65.0 of the REST API. A call to /services/data/v50.0/sobjects/Account uses version 50.0, even though the org runs a newer release.
The project-level setting
In a Salesforce DX project, sourceApiVersion in sfdx-project.json sets the API version that the CLI uses when it retrieves and deploys source.
{
"packageDirectories": [
{
"path": "force-app",
"default": true
}
],
"sourceApiVersion": "65.0"
}This setting matters in two ways. First, when you retrieve metadata, the CLI writes files in the format of that version. Second, it determines which metadata types and fields the CLI understands when it deploys. If you set it higher than the target org supports, the deploy fails. A production org that has not yet been upgraded cannot accept version 66.0 metadata.
That second point has a practical effect during the release window. A preview sandbox is already on the next release, while production is still on the current one. If you retrieve from the preview sandbox with a raised sourceApiVersion and then try to deploy to production, you can hit errors. Keep a deliberate separation between work for the current release and work for the next one.
Why versions drift
Versions in an org drift for ordinary reasons.
- Developers copy an old class as a starting point, and the copy keeps the old version.
- Tools such as the Developer Console or the VS Code extensions assign the current version to new components. Older components stay where they were.
- Managed packages ship with the version the vendor chose.
- Integrations are built once and never touched again.
Drift is not a problem by itself. It becomes one when a retired version stops working, or when a component needs a feature that only newer versions provide.
Version retirement
Salesforce retires very old API versions on a published schedule. When a version is retired, calls to it fail. For REST and SOAP, the failure is an error response from the endpoint, so an integration that worked for years can stop working on a given date.
Plan for this in three steps:
- Inventory the versions your integrations call.
- Compare them with the retirement notices in the release notes and in Salesforce's announcements.
- Schedule upgrades for any integration that is close to the retirement line.
To find which versions external callers use, review the API usage data your org records. In Setup, the Event Monitoring and API Total Usage tools show which API versions and clients make calls, depending on your edition and licenses. If you do not have those tools, check the integration code and its configuration directly, and ask the teams that own each integration.
Upgrading a version on purpose
Raising a component's API version is a code change and deserves the same care as any other. Behavior can differ between versions. The release notes describe version-dependent changes, and the Apex developer documentation lists behavior that applies only from a given version onward.
A safe upgrade process looks like this:
- Choose a small set of components to upgrade first, such as classes with strong test coverage.
- Change the
apiVersionin the metadata file in a source-controlled branch. - Deploy to a sandbox that is on the target release.
- Run the tests.
- Check the behavior manually for components that tests do not cover.
- Promote through your normal pipeline.
Run the Apex tests with the CLI. This command runs all local tests in a preview sandbox and waits up to 20 minutes for results:
sf apex run test \
--target-org my-preview-sandbox \
--test-level RunLocalTests \
--result-format human \
--wait 20Passing tests give you evidence, not proof. If a class has weak tests, a version change can alter behavior without any test failing. Add tests before you upgrade such a class.
Version-dependent behavior in practice
Some changes are tied to a version, and some apply to every version once the release arrives. The difference decides how much testing you need.
Version-dependent changes apply only to components at or above a given version. Your old components keep the old behavior until you raise their version. This is the safe case, and the risk appears later, when you do the upgrade.
Changes that apply regardless of version affect all components as soon as the org is upgraded. Release Updates, covered in an earlier chapter, are the main example. Security fixes and some platform changes also fall here. Pinning to an old version does not protect you from these.
Your impact assessment should therefore ask two separate questions for each item in the release notes: does it depend on the version, and when does it take effect?
Common errors
These problems appear often when teams manage versions.
- Deploy fails with an unsupported version error. The
sourceApiVersionor a component version is higher than the target org supports. Lower it, or wait until the target org is upgraded. - Retrieved files show many differences. Retrieving from an org on a newer release can add fields or change formatting. Review the diff before you commit.
- Raising a version breaks a test. Behavior changed between versions. Read the release notes between the two versions and fix the code, or keep the old version until you can.
- An integration fails with no code change. A retired API version is the likely cause. Check the version in the endpoint URL.
- Managed package components behave differently. You cannot change the version of a component in an installed managed package. Ask the vendor for an update.
A practical routine
Fit version housekeeping into your release cycle.
- During preview, review the new API version number and the retirement notices.
- Keep
sourceApiVersionon the current production release until production upgrades. - After the production upgrade, raise
sourceApiVersionin a branch, retrieve, and test. - Each release, upgrade a small batch of old components.
- Record the oldest version in use, so that drift stays visible.
You can find the oldest versions in a project quickly by searching the metadata files:
grep -rhoE "<apiVersion>[0-9.]+</apiVersion>" force-app \
| sort | uniq -c | sort -k2 -t'>' -nThe command prints each version with a count of components that use it. A long tail of old versions is a signal to schedule upgrades.
Exercise
Your team retrieves metadata from a preview sandbox with sourceApiVersion set to the next release's version, then tries to deploy to production, which has not yet been upgraded. The deploy fails. Why, and what should you do?
Show answer
Production is still on the current release, so it does not support metadata at the newer API version. Lower sourceApiVersion to the current production version for work that will deploy to production, and keep next-release work separate until production upgrades.
Exercise
A release note describes a behavior change that applies only to Apex classes at the new API version or higher. Does it affect your existing classes when the org upgrades? When should you test it?
Show answer
No. Existing classes keep their old version and old behavior. Test the change when you plan to raise the version of those classes, preferably in a sandbox on the target release with tests that cover the affected code.