Wednesday, May 25, 2016

Salesforce: Automate Assigning Managed Package Licenses

When you purchase 3rd party app by License (from AppExchange or others), once installed in Production, you will find the Packaged in Setup | Installed Packages. For free package, it will be available for all users, but for license by user, you will notice Manage Licenses link next to the Package Name. Then admin or user with Manage Package Licenses permission need to add users for particular license. This is simple job, but when you have new user onboard and offboard over the times, this activity often forget and become not practical.

Can we automate this? 
Since Summer 14 release, Salesforce make the object available via API, where you can use Process Builder + Flow or Trigger to automate this. There are 2 object you need to note.

1. PackageLicense
Represents a license for an installed managed package.
This object only support record query, not create, update, and delete.

2. UserPackageLicense
Represents a license for an installed managed package, assigned to a specific user.
This object support record create, delete, query, but not update.

I'll not go down to the detail on how to create Process Builder + Flow or Trigger code in this blog, but the idea is based on objects above, you need to get the PackageLicense Id, the create new record in UserPackageLicense, you just need to provide PackageLicenseId and UserId.

Sample Query
SELECT Id, NamespacePrefix, AllowedLicenses, UsedLicenses FROM PackageLicense WHERE Status = 'Active' AND AllowedLicenses > 0 ORDER BY NamespacePrefix

SELECT Id, PackageLicenseId, UserId FROM UserPackageLicense WHERE PackageLicenseId = '050o0000000DC8JAAW'

Reference: Assigning Licenses Using the API