function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PNSPNS 

Getting the API version from Java

Hi...

 

Is there any Java programmatic way of retrieving the version of the Salesforce API one uses for their development? There is a getApiVersion() method in the ApexClass of the Enterprise API but I am using the Partner API which does not seem to offer something similar. A PackageVersion class in both the Enterprise and the Partner APIs seems irrelevant to this purpose.

 

Also, is there any Java programmatic way of retrieving the current API version (e.g., say someone uses API v20 and Salesforce has released API v21)?

 

Thanks!

Steven LawranceSteven Lawrance

To get the current API version, it's possible to use regular http or https to request /servlet/Version, divide only the whole number part number by 2, and then subtract 64 from it. As an example, https://na1.salesforce.com/servlet/Version returns 170.0. 170 / 2 = 85, and 85 - 64 = 21. Copy the .0 part as-is from 170.0 to 21, and you have 21.0.

 

With that said, there aren't many use cases where a program would need to know what the latest API version number is. A program that was written for a specific API version can continue to use that API version in perpetuity, and the behaviors of that API version will remain constant, including when new Salesforce.com releases happen. If a program wants to log a message that informs the developer that a newer version of the API is available, then that could be a potential use case.

 

Note that if a program gets the version number from /servlet/Version and then uses that with an older WSDL file to access the API, then it is likely that some or all API calls will fail in unexpected ways, such as receiving error messages about unknown SOAP object or field types. Data will be safe, but either the API request or the response from Salesforce.com could run into SOAP schema problems.

Steven LawranceSteven Lawrance

Alternatively, the program can download the current partner or enterprise WSDL file from the URL exposed in the setup, and that WSDL contains the version information within the file. This is probably the best supported way other than exposing an Apex web service that returns the result of getApiVersion().

 

I should note that /servlet/Version is not an officially supported way to get the version number, so anything that uses it should be designed to continue to work if /servlet/Version someday returns the API version number directly or a generic 404-not-found page.

SuperfellSuperfell

Don't use /servlet/version. Use the REST API, the root REST API resource at /services/data returns information about versions, e.g. if you've been upgraded to spring'11 it'll return

 

 

<?xml version="1.0" encoding="UTF-8"?>
<Versions>
    <Version>
        <label>Winter &apos;11</label>
        <url>/services/data/v20.0</url>
        <version>20.0</version>
    </Version>
    <Version>
        <label>Spring &apos;11</label>
        <url>/services/data/v21.0</url>
        <version>21.0</version>
    </Version>
</Versions>

 

 

Having said all that, the versioning support in salesforce is designed so that you don't need to care, you build you client against a specific API version, and that API version continues to behave that way regardless of upgrades and new API versions, you only need to change to a newer API version if you want to take advantage of something new in that API version.

 

PNSPNS

Interesting ideas... It seems a bit odd to me that both the Enterprise and the Partner APIs lack two simple methods to that effect, i.e. something like getCurrentVersion() and getLatestVersion(). Granted, they are not needed because of the clever way Salesforce offers versioning independence, but I would rather they existed, at least for informational purposes.

 

Being rather at the conclusion of my Salesforce project, I just use a simple parsing of the server URL (returned from the LoginResult) to identify the version number (e.g., 20.0) and the full class name of the SObject class (i.e., new SObject().getClass().getCanonicalName()) to identify the API name.

 

Thanks to everyne for the suggestions!