[SalesForce] Get the list of users licensed for a Managed Package

I'm developing a managed package and I need the list of users with the license for the managed package within the package.

Any idea on where to start will help!

Overriding a standard button depending on whether user is licensed

This gave me some insight that I cannot use UserInfo object within the app. Am I on the right path?

Best Answer

As of Summer 14, there are 2 new objects which allow you to use SOQL to see which users are licensed for particular Managed Packages: PackageLicense and UserPackageLicense.

UserPackageLicense lets you see which users are licensed for a particular package:

String APP_NAMESPACE_PREFIX = 'skuid';

List<User> licensedUsers = [
   SELECT Name 
   FROM User 
   WHERE Id in (
       SELECT UserId 
       FROM UserPackageLicense 
       WHERE (PackageLicense.NamespacePrefix = :APP_NAMESPACE_PREFIX)
   )
];

The UserPackageLicense object supports DML as well, so you could add triggers on User after insert/update that either assign available licenses on User activation or revoke licenses on User deactivation.

The PackageLicense object lets you see the stats on the number of licensed users available and currently in use in your org for a particular package, as well as when the app was installed and when it expires:

PackageLicense packageLicensing = [
   SELECT AllowedLicenses, UsedLicenses, 
           ExpirationDate, CreatedDate, 
           IsProvisioned, Status
   FROM PackageLicense 
   WHERE NamespacePrefix = :APP_NAMESPACE_PREFIX
   LIMIT 1
];
Related Topic