[SalesForce] Get currently installed package version from Apex Class

In my managed package I have a Setup page that should only display setup steps relevant for the currently installed version of my package.

I know in InstallHandler implementations there are way to ask for the Version but how would I do that from a regular Apex class?

Best Answer

You can use System.requestVersion, which returns the version of your package. See Version for more details, but I'll also include the snippet from there here:

if (System.requestVersion() == new Version(1,0))
    // Do something
if ((System.requestVersion().major() == 1) 
     && (System.requestVersion().minor() > 0)
     && (System.requestVersion().minor() <=9))
    // Do something different for versions 1.1 to 1.9
else if (System.requestVersion().compareTo(new Version(2,0)) >= 0)
    // Do something completely different for versions 2.0 or greater
Related Topic