No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
PackageLicense
Represents a license for an installed managed package. This object is available in API version 31.0 and later.
Fields
| Field Name | Details |
|---|---|
| AllowedLicenses |
|
| ExpirationDate |
|
| NamespacePrefix |
|
| Status |
|
| UsedLicenses |
|
Usage
Use this object to determine the number of licenses allowed and in use for a managed package installed in your organization.
The following example demonstrates the use of the
API to manage licenses for a package. The example defines an Apex
class that does the following.
- Retrieves the PackageLicense record for the specified package (identified by its namespace prefix).
- Defines a function that returns a list of all users with the specified profile.
- Creates a UserPackageLicense record for each user with that profile, which has the effect of assigning a license for the package to all users with that profile.
- Returns an error message if the number of users exceeds the number of available licenses.
1public class AssignPackageLicense {
2
3 static String PACKAGE_NAMESPACE_PREFIX = 'acme_101';
4 static String PROFILE_ID = '00exx000000jz1SAAQ';
5 public static String exceptionText {get; set;}
6
7 public AssignPackageLicense() {
8 exceptionText = 'Initialized';
9 }
10
11 static List<User> getUsersWithProfile(){
12 String userQuery = 'SELECT Id FROM User WHERE ProfileId = :PROFILE_ID';
13 List<User> matchingUsers = new List<User>();
14 matchingUsers = [SELECT Id FROM User WHERE ProfileId = :PROFILE_ID];
15 return matchingUsers;
16 }
17
18 public static void assignLicenseByProfile() {
19 //find the PackageLicense Id
20 PackageLicense pl = [SELECT Id, NamespacePrefix, AllowedLicenses, UsedLicenses,
21 ExpirationDate,Status FROM PackageLicense WHERE
22 NamespacePrefix = :PACKAGE_NAMESPACE_PREFIX];
23 System.assert(pl != null, 'PackageLicense cannot be null.');
24 List<User> usersToAssignLicenses = getUsersWithProfile();
25 List<UserPackageLicense> firstUPLs = new List<UserPackageLicense>();
26
27 //create a new UserPackageLicense record for each user with the specified profile
28 for (Integer i = 0; i< usersToAssignLicenses.size(); i++){
29 UserPackageLicense upl = new UserPackageLicense();
30 upl.PackageLicenseId = pl.Id;
31 upl.UserId = usersToAssignLicenses[i].Id;
32 firstUPLs.add(upl);
33 }
34
35 try {
36 //bulk insert
37 insert(firstUPLs);
38 } catch(DmlException e) {
39 for (Integer i = 0; i < e.getNumDml(); i++) {
40 // process exception here
41 System.debug(e.getDmlMessage(i));
42 String status = e.getDmlStatusCode(i);
43 System.debug(status + ' ' + e.getDmlMessage(i));
44 if(status.equals('LICENSE_LIMIT_EXCEEDED')){
45 exceptionText = 'You tried to assign more licenses than available. '
46 +' You tried to create '+ firstUPLs.size()+' licenses but only have '
47 + (pl.AllowedLicenses - pl.UsedLicenses) + ' licenses free.';
48 System.debug(exceptionText);
49 }
50 }
51 }
52 }
53 }