Apex フックを使用した B2B ストア価格設定のカスタマイズ
独自の価格設定シナリオをサポートするには、カスタムApexロジックを価格設定手順プランに追加します。Apex フックを使用して、価格設定手順の実行前または実行後に価格設定コンテキストを変更するカスタム ビジネス ロジックを適用できます。Apexフックは、価格設定手順プランの特定のポイントで実行されるカスタム コードです。
必要なエディション
-
[設定] から、[クイック検索] ボックスに「
Apex」と入力し、[Apex クラス] を選択します。 - [新規] を選択して Apex クラスを作成します。
- クラスエディターで、クラス定義を入力します。このトピックで提供されている Apex フックのサンプルクラスを参照してください。
-
Apex クラスを作成したら、手順プラン定義の Apex セクションとして追加します。[設定] から、[クイック検索] ボックスに「
Procedure Plan Definitions」と入力し、[処置プラン定義] を選択します。 - 変更する処置プランを選択します。
- [処置計画セクション] 領域で、[追加] をクリックします。
- [標準種別] を選択します。
- セクションの名前を入力します。
- [セクション種別] で、Apex を選択します。
- セクションの詳細で、[フェーズ]を[価格設定]、[解決種別]を[デフォルト]に設定し、作成したApexクラスを選択します。
- [保存] をクリックします。
- 複数のセクションを追加した場合は、[セクションを管理] をクリックします。セクションをドラッグして、価格設定手順に対する正確な実行順序を設定します。
Apex フックのサンプルクラス
Apex 事前フック: この例は、SalesTransactionItem ノードでの属性駆動の価格設定の Apex 事前フックを示しています。
global class ApexDmlAttributePreHook implements RevSignaling.SignalingApexProcessor {
public RevSignaling.TransactionResponse execute(RevSignaling.TransactionRequest request) {
System.debug('Executing PREHOOK');
String contextId = request.ctxInstanceId;
Context.IndustriesContext industriesContext = new Context.IndustriesContext();
// STEP 1 - Query SalesTransactionItem nodes
Map<String, Object> inputQueryItem = new Map<String, Object>{
'contextId' => contextId,
'tags' => new List<String>{ 'SalesTransactionItem' }
};
Map<String, Object> itemQueryOutput = industriesContext.queryTags(inputQueryItem);
Map<String, Object> itemQueryResult = (Map<String, Object>) itemQueryOutput.get('queryResult');
List<Object> itemData = (List<Object>) itemQueryResult.get('SalesTransactionItem');
// STEP 2 - Build update list for items with ProductName = 'Home Office Starter'
List<Map<String, Object>> itemNodeUpdates = new List<Map<String, Object>>();
for (Object itemObj : itemData) {
Map<String, Object> itemNode = (Map<String, Object>) itemObj;
Map<String, Object> tagMap = (Map<String, Object>) itemNode.get('tagValue');
List<Object> dataPath = (List<Object>) itemNode.get('dataPath');
String productName = null;
if (tagMap != null && tagMap.containsKey('ProductName')) {
productName = (String) ((Map<String, Object>) tagMap.get('ProductName')).get('tagValue');
}
if (productName == 'Home Office Starter') {
System.debug('Matched Home Office Starter item: ' + JSON.serialize(dataPath));
// Remove root contextId from dataPath
dataPath.remove(0);
itemNodeUpdates.add(new Map<String, Object>{
'nodePath' => new Map<String, Object>{
'dataPath' => dataPath
},
'attributes' => new List<Object>{
new Map<String, Object>{
'attributeName' => 'ProductCustom__c',
'attributeValue' => 'DISCOUNTAPPLICABLE'
}
}
});
}
}
// STEP 3 - Submit context update
if (!itemNodeUpdates.isEmpty()) {
Map<String, Object> updateInput = new Map<String, Object>{
'contextId' => contextId,
'nodePathAndAttributes' => itemNodeUpdates
};
System.debug('--- PREHOOK: SUBMITTING CONTEXT UPDATE ---');
System.debug(JSON.serializePretty(updateInput));
industriesContext.updateContextAttributes(updateInput);
}
RevSignaling.TransactionResponse response = new RevSignaling.TransactionResponse();
response.status = RevSignaling.TransactionStatus.SUCCESS;
return response;
}
}
Apex ポストフック:この例では、価格設定属性を検証して選択的に上書きする Apex ポストフックを示します。
global class ApexDmlListPricePostHook implements RevSignaling.SignalingApexProcessor {
public RevSignaling.TransactionResponse execute(RevSignaling.TransactionRequest request) {
System.debug('Executing POSTHOOK - Update ListPrice');
String contextId = request.ctxInstanceId;
Context.IndustriesContext industriesContext = new Context.IndustriesContext();
// STEP 1 - Query SalesTransactionItem nodes
Map<String, Object> inputQueryItem = new Map<String, Object>{
'contextId' => contextId,
'tags' => new List<String>{ 'SalesTransactionItem' }
};
Map<String, Object> itemQueryOutput = industriesContext.queryTags(inputQueryItem);
Map<String, Object> itemQueryResult = (Map<String, Object>) itemQueryOutput.get('queryResult');
List<Object> itemData = (List<Object>) itemQueryResult.get('SalesTransactionItem');
// STEP 2 - Build update list
List<Map<String, Object>> itemNodeUpdates = new List<Map<String, Object>>();
for (Object itemObj : itemData) {
Map<String, Object> itemNode = (Map<String, Object>) itemObj;
List<Object> dataPath = (List<Object>) itemNode.get('dataPath');
// Remove root contextId as required by updateContextAttributes
dataPath.remove(0);
itemNodeUpdates.add(new Map<String, Object>{
'nodePath' => new Map<String, Object>{
'dataPath' => dataPath
},
'attributes' => new List<Object>{
new Map<String, Object>{
'attributeName' => 'ListPrice',
'attributeValue' => 20.0
}
}
});
}
// STEP 3 - Submit context update
if (!itemNodeUpdates.isEmpty()) {
Map<String, Object> updateInput = new Map<String, Object>{
'contextId' => contextId,
'nodePathAndAttributes' => itemNodeUpdates
};
System.debug('--- POSTHOOK: SUBMITTING CONTEXT UPDATE ---');
System.debug(JSON.serializePretty(updateInput));
industriesContext.updateContextAttributes(updateInput);
}
RevSignaling.TransactionResponse response = new RevSignaling.TransactionResponse();
response.status = RevSignaling.TransactionStatus.SUCCESS;
return response;
}
}
この記事で問題は解決されましたか?
ご意見をお待ちしております。

