I have a few classes that need to be kept at the same Salesforce API version since they interact with Salesforce metadata that may change between API versions. Because of this I would like to be able to create a unit test that ensures that these classes are using the same API version. This would prevent us from packaging when the class API versions are not the same.

Is there a way to dynamically get the Salesforce API version of a class using Apex?

2 Answers 2


You can perform a SoQL query on the ApexClass object:

ApexClass apexClass = [
  SELECT ApiVersion
  FROM ApexClass
  WHERE Name = 'MyClassName'


Don't worry, the records will be visible without needing SeeAllData=true


You can also check the API version from the developer console.

SELECT Id, NamespacePrefix, Name, ApiVersion, Status FROM ApexClass where NamespacePrefix = ''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.