3

Is it possible to query the version number of an installed managed package? I do not find any information in the PackageLicense object. If not, what is the alternative approach?

6

From within managed code, you can use the Version interface to determine the current version. It involves making use of System.requestVersion() to get the current version that's running, and provides an example of how to use it, included 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
}

Outside of managed code, you'd have to use the Metadata API to get the installed version. This would be non-trivial, but is possible. The Apex Metadata API might be of some use to you.

2

As sfdcfox mentioned, finding the version number using Metadata API was not anywhere close to trivial:), got some code finally running after many attempts, posting the code here : Download MetadataService class from [https://github.com/financialforcedev/apex-mdapi/tree/master/apex-mdapi/src/classes]

MetadataService.MetadataPort service = new MetadataService.MetadataPort();

service.SessionHeader = new MetadataService.SessionHeader_element();

service.SessionHeader.sessionId = UserInfo.getSessionId();

System.debug('sessionId'+service.SessionHeader.sessionId);

List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();        

MetadataService.ListMetadataQuery queryLayout = new MetadataService.ListMetadataQuery();

queryLayout.type_x = 'InstalledPackage';
queries.add(queryLayout);       

MetadataService.FileProperties[] fileProperties1 = service.listMetadata(queries, 34);

List<String> packageNames = new List<String>(); 
for (MetadataService.FileProperties n : fileProperties1) {

   if(n.fullName=='ts2')
    packageNames.add(n.fullName);
}

List<MetadataService.InstalledPackage> installPackageInfo = 
    (List<MetadataService.InstalledPackage>) 
    service.readMetadata('InstalledPackage', packageNames).getRecords();

for(MetadataService.InstalledPackage a:installPackageInfo)
{

  System.debug(a.fullName +'*****'+a.versionNumber);
}

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.