高度なポリシー移行の例
この例では、より複雑なポリシーを移行する方法を説明します。
| 使用可能なインターフェース: Salesforce Classic および Lightning Experience |
| 使用可能なエディション: Enterprise Edition、Unlimited Edition、および Developer Edition Salesforce Shield または Salesforce Event Monitoring アドオンサブスクリプションが必要です。 |
このトピックの従来のサンプルポリシーは、リードデータエクスポートポリシーに似ていますが、2 つの重要な違いがあります。このポリシーは、リードのみを監視するのではなく、複数の異なるオブジェクト種別を監視します。また、エクスポート制限の 2,000 レコードをハードコード化するのではなく、異なるオブジェクト種別ごとに異なる制限を定義します。
エクスポート制限は、TransactionSecurityLimit__mdt というカスタムメタデータ型に保存されます。この型には次の 2 つの項目が含まれます。
- Object_Type__c (選択リスト) — オブジェクト種別
- Limit_Value__c (数値 (18,0)) — このオブジェクト種別でユーザにエクスポートが許可される最大レコード数
従来のポリシーは、このカスタムメタデータ型を照会して各オブジェクト種別のエクスポート制限値を動的に判断します。
1global class DataExportPolicyCondition implements TxnSecurity.PolicyCondition {
2
3 private Final Integer DEFAULT_LIMIT = 2000;
4
5 public boolean evaluate(TxnSecurity.Event e){
6 Integer numberOfRecords = Integer.valueOf(e.data.get('NumberOfRecords'));
7 String entityName = e.data.get('EntityName');
8
9 Integer limitValue = getLimitValue(entityName);
10
11 if (numberOfRecords > limitValue) {
12 return true;
13 }
14 return false;
15 }
16
17 /**
18 * Get the export limit for the given object type. If no such limit exists,
19 * or an exception occurs while trying to look up the limit, the default limit
20 * of 2000 records is returned.
21 **/
22 private Integer getLimitValue(String entityName) {
23
24 List<Transaction_Security_Limit__mdt> limits = new List<Transaction_Security_Limit__mdt>();
25
26 try {
27 limits = [SELECT Limit_Value__c FROM Transaction_Security_Limit__mdt WHERE Object_Type__c = :entityName];
28 } catch (Exception ex) {
29 // unable to determine the limit, log and return the default
30 System.debug('Error getting limit value\n: ' + ex.getMessage());
31 return DEFAULT_LIMIT;
32 }
33
34 if (limits.size() == 0) {
35 // no limit found, return the default
36 return DEFAULT_LIMIT;
37 }
38
39 return (Integer)(limits[0].Limit_Value__c);
40 }
41}拡張ポリシーでは、TransactionSecurityLimit__mdt カスタムメタデータ型を照会するロジックの大半を再利用できます。主な違いは、エクスポート制限を照会する対象のエンティティの��前を取得するコードです。従来のポリシーでは data Map の EntityName キー値を使用します。拡張フレームワークでこれに相当するのが QueriedEntities です。ただし、拡張フレームワークではすべての標準オブジェクトとカスタムオブジェクトでエクスポートがサポートされるため、QueriedEntities 項目に複数のエンティティ名が含まれる可能性があります。そのため、照会されるエンティティのカンマ区切りリストを取り込み、エンティティ名の List に分離します。
1global class DynamicExportEventCondition implements TxnSecurity.EventCondition {
2
3 private Final Integer DEFAULT_LIMIT = 2000;
4
5 public boolean evaluate(SObject event) {
6 switch on event{
7 when ApiEvent apiEvent {
8 return evaluate(apiEvent.QueriedEntities, apiEvent.RowsProcessed);
9 }
10 when ReportEvent reportEvent {
11 return evaluate(reportEvent.QueriedEntities, reportEvent.RowsProcessed);
12 }
13 when ListViewEvent listViewEvent {
14 return evaluate(listViewEvent.QueriedEntities, listViewEvent.RowsProcessed);
15 }
16 when null {
17 return false;
18 }
19 when else {
20 return false;
21 }
22 }
23 }
24
25 private boolean evaluate(String queriedEntities, Decimal rowsProcessed){
26 List<String> queriedEntitiesList = queriedEntities.split(',');
27 // for all of the entities being exported, check their limit
28 for (String queriedEntity : queriedEntitiesList) {
29 Integer limitValue = getLimitValue(queriedEntity);
30 if (rowsProcessed > limitValue) {
31 // if any of our entities are having their limit violated
32 // then return true to trigger the policy
33 return true;
34 }
35 }
36 return false;
37 }
38
39 /**
40 * Get the export limit for the given object type. If no such limit exists,
41 * or an exception occurs while trying to look up the limit, the default limit
42 * of 2000 records is returned.
43 **/
44 private Integer getLimitValue(String entityName) {
45
46 List<Transaction_Security_Limit__mdt> limits = new List<Transaction_Security_Limit__mdt>();
47
48 try {
49 limits = [SELECT Limit_Value__c FROM Transaction_Security_Limit__mdt WHERE Object_Type__c = :entityName];
50 } catch (Exception ex) {
51 // unable to determine the limit, return the default
52 System.debug('Error getting limit value\n: ' + ex.getMessage());
53 return DEFAULT_LIMIT;
54 }
55
56 if (limits.size() == 0) {
57 // no limit found, return the default
58 return DEFAULT_LIMIT;
59 }
60
61 return (Integer)(limits[0].Limit_Value__c);
62 }
63}