Loading
Slack製品の問い合わせ先はSalesforce Helpへ統合・変更されます続きを読む
Apex 価格設定フックのベストプラクティス

Apex 価格設定フックのベストプラクティス

価格設定手順プランに Apex フックを実装するときは、次のベストプラクティスに従ってパフォーマンスを最適化し、予期しない結果を回避します。

Apex ガバナ制限

処置プランの Apex フックは同じ Apex トランザクション内で実行されるため、中央処理装置タイマーはすべての操作で累積されます。フックロジックを記述するときは、特に品目数の多い取引ではガバナ制限に注意してください。

現在の制限値については、『Apex 開発者ガイド』の「Execution Governors and Limits」を参照してください。

必要なタグのみのクエリ

項目ごとにすべての属性を返す SalesTransactionItem などの広範なエンティティレベルのタグを照会しないでください。代わりに、フックロジックで必要な特定のタグのみを要求します。

非推奨: ブロードタグの照会


// BAD: Returns ALL attributes for every SalesTransactionItem
Map<String, Object> input = new Map<String, Object>{
    'contextId' => contextId,
    'tags' => new List<String>{ 'SalesTransactionItem' }
};
Map<String, Object> output = industriesContext.queryTags(input);
   

推奨: 特定のタグのみを照会する


// GOOD: Query only the specific tags (attributes) you need
Map<String, Object> input = new Map<String, Object>{
    'contextId' => contextId,
    'tags' => new List<String>{ 'LineItem', 'LineItemQuantity', 'ItemProductCode' }
};
Map<String, Object> output = industriesContext.queryTags(input);
   

推奨: 特定のタグと leanerQueryTags を組み合わせる


// BETTER: Combine specific tags with leanerQueryTags for maximum efficiency
Map<String, Object> input = new Map<String, Object>{
    'contextId' => contextId,
    'tags' => new List<String>{ 'LineItem', 'LineItem$DmlStatus',
        'LineItemQuantity', 'ItemProductCode' }
};
Map<String, Object> output = industriesContext.leanerQueryTags(input);
   

queryTags の代わりに leanerQueryTags を使用

Context.IndustriesContext クラスには、コンテキストデータを照会するための 2 つのメソッドがあります。パフォーマンスを向上するには、可能な限り leanerQueryTags を使用します。

queryTags (ヘビー) — ネストされたタグ値マップとdataPath配列を含むフルコンテキストノードを返します。すべての属性は、項目ごとに属性ごとに 6 つの項目 (tagValue、dmlStatus、isDirty、isNodeLevelTag、tagPath、contextDataPathBuilder) を含む対応付けでラップされます。何が必要かに関係なく、タグのすべての属性が返されます。

leanerQueryTags (軽量) — フラットタグ値とインデックスベースのレコード ID 参照を含むコンパクト構造を返します。タグ値には 3 つの項目 (tagValue、recordIdIndexesForPath、isNodeLevelTag) のみが含まれます。レコード ID は、共有recordsInfoリストに重複排除され、ノードはインデックスで参照します。

次の例は、leanerQueryTags をコールしてその出力を処理する方法を示しています。


String contextId = request.ctxInstanceId;
Context.IndustriesContext industriesContext = new Context.IndustriesContext();

Map<String, Object> input = new Map<String, Object>{
    'contextId' => contextId,
    'tags' => new List<String>{ 'LineItem', 'ItemProductCode', 'LineItemQuantity' }
};
Map<String, Object> output = industriesContext.leanerQueryTags(input);

// Access the leaner result structure
Map<String, Object> queryResult = (Map<String, Object>)
    output.get('leanerQueryTagResult');
List<Object> recordsInfo = (List<Object>) output.get('recordsInfo');
List<Object> lineItems = (List<Object>) queryResult.get('LineItem');

// Iterate over tag values — each tag is flat
List<Object> productCodes = (List<Object>) queryResult.get('ItemProductCode');
for (Object obj : productCodes) {
    Map<String, Object> tagNode = (Map<String, Object>) obj;
    String productCode = (String) tagNode.get('tagValue');
    List<Integer> indexes = (List<Integer>) tagNode.get('recordIdIndexesForPath');
    // recordsInfo entries are maps with 'recordId' and 'dmlStatusOrdinal'
    String itemId = (String) ((Map<String, Object>)
        recordsInfo.get(indexes[1])).get('recordId');
}
   

leanerQueryTagsの出力構造は次のようになります。


{
  "isSuccess": true,
  "contextId": "...",
  "leanerQueryTagResult": {
    "LineItem": [
      { "recordIdIndexesForPath": [0, 1], "tagValue": "0QLVW000001LxvN4AS", "isNodeLevelTag": false },
      { "recordIdIndexesForPath": [0, 2], "tagValue": "0QLVW000001Lxus4AC", "isNodeLevelTag": false }
    ],
    "ItemProductCode": [
      { "recordIdIndexesForPath": [0, 1], "tagValue": "PROD-001", "isNodeLevelTag": false },
      { "recordIdIndexesForPath": [0, 2], "tagValue": "PROD-002", "isNodeLevelTag": false }
    ]
  },
  "recordsInfo": [
    { "recordId": "0Q0VW0000011qC50AI", "dmlStatusOrdinal": null },
    { "recordId": "0QLVW000001LxvN4AS", "dmlStatusOrdinal": null },
    { "recordId": "0QLVW000001Lxus4AC", "dmlStatusOrdinal": null }
  ]
}
   
メモ
メモ 既存の Apex 価格設定フックを更新して、可能な場合は queryTag ではなく leanerQueryTag を使用します。

queryTags から leanerQueryTags への移行

既存のフックを queryTags から leanerQueryTags に移行するときは、次のキー変換を適用します。

入力: ブロードタグの代わりに個々のタグを使用


// Before (queryTags — broad tag):
Map<String, Object> input = new Map<String, Object>{
    'contextId' => contextId,
    'tags' => new List<String>{ 'SalesTransactionItem' }
};
Map<String, Object> output = industriesContext.queryTags(input);
   

// After (leanerQueryTags — specific tags):
Map<String, Object> input = new Map<String, Object>{
    'contextId' => contextId,
    'tags' => new List<String>{ 'LineItem', 'LineItem$DmlStatus',
        'ItemProductCode', 'LineItemQuantity' }
};
Map<String, Object> output = industriesContext.leanerQueryTags(input);
   

出力: 異なる最上位キー


// Before:
Map<String, Object> queryResult = (Map<String, Object>) output.get('queryResult');
List<Object> items = (List<Object>) queryResult.get('SalesTransactionItem');
   

// After:
Map<String, Object> queryResult = (Map<String, Object>)
    output.get('leanerQueryTagResult');
List<Object> recordsInfo = (List<Object>) output.get('recordsInfo');
List<Object> lineItems = (List<Object>) queryResult.get('LineItem');
   

ノードアクセス: ネストされた map ではなくフラットな値


// Before (queryTags — nested map extraction):
for (Object itemObj : items) {
    Map<String, Object> itemNode = (Map<String, Object>) itemObj;
    Map<String, Object> tagValueMap = (Map<String, Object>) itemNode.get('tagValue');
    String productCode = (String) ((Map<String, Object>)
        tagValueMap.get('ItemProductCode')).get('tagValue');
    Decimal quantity = (Decimal) ((Map<String, Object>)
        tagValueMap.get('LineItemQuantity')).get('tagValue');
}
   

// After (leanerQueryTags — flat value, iterated per tag):
List<Object> productCodes = (List<Object>) queryResult.get('ItemProductCode');
List<Object> recordsInfo = (List<Object>) output.get('recordsInfo');

for (Object obj : productCodes) {
    Map<String, Object> tagNode = (Map<String, Object>) obj;
    String productCode = (String) tagNode.get('tagValue');
    List<Integer> indexes = (List<Integer>) tagNode.get('recordIdIndexesForPath');
    String itemId = (String) ((Map<String, Object>)
        recordsInfo.get(indexes[1])).get('recordId');
}
   

レコード ID: dataPath ではなくインデックス参照


// Before (queryTags):
List<Object> dataPath = (List<Object>) itemNode.get('dataPath');
// dataPath = ['contextId', 'txnId', 'itemId'] — 3 elements, first is contextId
dataPath.remove(0);
String transactionId = (String) dataPath.get(0);
   

// After (leanerQueryTags):
List<Object> recordsInfo = (List<Object>) output.get('recordsInfo');
List<Integer> indexes = (List<Integer>) tagNode.get('recordIdIndexesForPath');
String transactionId = (String) ((Map<String, Object>)
    recordsInfo.get(indexes[0])).get('recordId');
String itemId = (String) ((Map<String, Object>)
    recordsInfo.get(indexes[1])).get('recordId');
   

tagIdIndexMap パターンの使用

1 つのループで複数のタグを処理する場合、tagIdIndexMap を使用して各タグ名を recordIdIndexesForPath 配列内の位置に対応付けます。これにより、各階層レベルで ID を正しく抽出できます。


Map<String, Integer> tagIdIndexMap = new Map<String, Integer>{
    'LineItem' => 1,            // item-level tag, ID at index 1
    'LineItem$DmlStatus' => 1,  // item-level tag, ID at index 1
    'LineItemQuantity' => 1,    // item-level tag, ID at index 1
    'BusinessUnit__c' => 0,     // transaction-level tag, ID at index 0
    'Contract' => 0             // transaction-level tag, ID at index 0
};
   
  • インデックス 0 = トランザクションレベル (パスには 1 つの要素 [txnId])
  • インデックス 1 = 項目レベル (パスには [txnId, itemId] の 2 つの要素があります)
  • インデックス 2 = 詳細レベル(パスには [txnId、itemId、detailId] の 3 つの要素があります)

削除済み項目の絞り込み

不要な処理を回避するために、反復ループの開始時に削除された項目をスキップします。クエリの個別のタグとして要求 LineItem$DmlStatus します。


private static final String DML_DELETED = 'DELETED';

for (Map<String, Object> item : salesTransactionInstance.salesTransactionItems) {
    String dmlStatus = (String) item.get('LineItem$DmlStatus');

    if (dmlStatus == DML_DELETED) {
        continue; // Skip deleted items
    }

    // Process active items...
}
   

個別のタグとしての $DmlStatus の参照

特定のタグを照会する場合 (推奨アプローチ)、DML 状況はタグメタデータに埋め込まれません。個別のタグとしてLineItem$DmlStatusを要求する必要があります。これは、queryTags と leanerQueryTags の両方に適用されます。


// Add LineItem$DmlStatus to your tags list
'tags' => new List<String>{ 'LineItem', 'LineItem$DmlStatus', 'ItemProductCode' }

// Access it like any other tag in the result
Map<String, Object> dmlNode = (Map<String, Object>) dmlStatusValues.get(i);
String dmlStatus = String.valueOf(dmlNode.get('tagValue'));
   
 
読み込み中
Salesforce Help | Article