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?

11

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

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.