위치:
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);
queryTags 대신 leanerQueryTags 사용
Context.IndustriesContext 클래스는 컨텍스트 데이터를 쿼리하는 두 가지 방법을 제공합니다. 보다 나은 성능을 위해 가능한 한 leanerQueryTags를 사용하십시오.
queryTags (heavy) - 중첩된 태그-값 맵 및 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 }
]
}
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');
}
레코드 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 패턴 사용
단일 루프에서 여러 태그를 처리하는 경우 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 = 트랜잭션 수준 (경로에 요소가 하나 있습니다: [
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'));
