10

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

12

You can perform a SoQL query on the ApexClass object:

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

System.debug(apexClass.ApiVersion);
//27.0

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

0

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .