PackageLicense
項目
| 項目名 | 詳細 |
|---|---|
| AllowedLicenses |
|
| ExpirationDate |
|
| NamespacePrefix |
|
| Status |
|
| UsedLicenses |
|
使用方法
このオブジェクトを使用して、組織にインストール済みの管理パッケージで許可されているライセンス数と使用中のライセンス数を決定します。
次の例では、API を使用してパッケージのライセンスを管理します。例では、次の操作を実行する Apex クラスを定義します。
- 指定されたパッケージの PackageLicense レコードを取得する (名前空間プレフィックスで識別)。
- 指定されたプロファイルを持つすべてのユーザのリストを返す関数を定義する。
- そのプロファイルを持つユーザごとに UserPackageLicense レコードを作成する。これにより、そのプロファイルを持つすべてのユーザにパッケージのライセンスが割り当てられます。
- ユーザ数が使用可能なライセンス数を超えたらエラーメッセージを返す。
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 }