您位於此處:
Apex 定價勾點的最佳作法
在定價程序計畫中實作 Apex 勾點時,請遵循下列最佳作法來最佳化效能並避免非預期的結果。
Apex 管理員限制
程序計畫中的 Apex 綁架會在相同的 Apex 交易中執行,因此中央處理單位計時器會累積所有作業。撰寫勾點邏輯時請注意管理員限制,特別是針對具有許多條列項目的交易。
如需目前限制值,請參閱《Apex 開發人員指南》中的 執行管理員與限制。
僅查詢您需要的標記
避免查詢廣泛的實體層級標記,例如 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);
使用 leanerQueryTags,而非 queryTags
Context.IndustriesContext 類別提供兩種方法來查詢內容資料。盡可能使用 leanerQueryTags 以獲得更好的效能。
queryTags (heavy) — 使用巢狀標記值地圖和 dataPath 陣列傳回完整內容節點。每個屬性都封裝在地圖中,每個屬性每個項目有 6 個欄位 (tagValue、dmlStatus、isDirty、isNodeLevelTag、tagPath、contextDataPathBuilder)。無論您需要什麼,系統都會傳回標記的所有屬性。
leanerQueryTags (輕量型) — 傳回具有平面標記值和索引型記錄識別碼參照的精簡結構。標記值僅有 3 個欄位 (tagValue、recordIdIndexesForPath、isNodeLevelTag)。記錄識別碼會移除為共用 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 }
]
}
從 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');
節點存取權:平面值而非巢狀地圖
// 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');
}
記錄識別碼:索引參照而非 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 模式
在單一迴圈中處理多個標記時,使用 tagIdIndexMap 將每個標記名稱對應至其在 recordIdIndexesForPath 陣列中的位置。這可讓您在每個階層層級正確地解壓縮識別碼:
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 = 交易層級 (路徑有一個元素:[
txnId]) - 索引 1 = 項目層級 (路徑有兩個元素:[
txnId、itemId]) - 索引 2 = 詳細資料層級 (路徑有三個元素:[
txnId、itemId、detailId])
篩選已刪除的項目
略過重複迴圈開始時刪除的項目,以避免不必要的處理。要求 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'));
