LWC の設定
LWC レイヤーの TPM プロモーションデータにアクセスするには、tpm-promotion サービスコンポーネントを使用します。tpm-promotion コンポーネントを使用すると、プロモーションの状態にフックし、プロモーションからのデータ抽出 (イベント経由) とプロモーションへの新規データのプッシュ (メソッド経由) ができるようになります。
- tpm-promotion component
-
1<!-- YourComponent.html --> 2<template> 3 <!-- Include the component in your markup --> 4 <cgcloud-tpm-promotion 5 data-id="fwk" 6 promotion-id={recordId} 7 <!-- Define event handlers for the data you need on your component --> 8 oneditmodechange={onEditModeChange} 9 ></cgcloud-tpm-promotion> 10 11 <!-- Sample button --> 12 <lightning-button variant="neutral" label="Toggle Edit Mode" onclick={handleEditModeClick}></lightning-button> 13</template> 14 15<!-- YourComponent.js --> 16import { api, LightningElement } from 'lwc'; 17 18export default class YourComponent extends LightningElement { 19 @api 20 recordId; // Record id is used to initialize the tpm-promotion component 21 22 onEditModeChange(event) { 23 // This event is called every time the Edit mode changes 24 // for the promotion 25 this.editMode = event.detail.value; 26 } 27 28 handleEditModeClick() { 29 // You can push changes/alter the state of the promotion 30 // by calling the component methods 31 // State changes will be notified to all the components of the page 32 this.template.querySelector('cgcloud-tpm-promotion') 33 .setEditMode(!this.editMode); 34 } 35}<!-- YourComponent.js-meta.xml --> 36<?xml version="1.0" encoding="UTF-8"?> 37<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> 38 <apiVersion>55.0</apiVersion> 39 <isExposed>true</isExposed> 40 <targets> 41 <target>lightning__RecordPage</target> 42 </targets> 43 <targetConfigs> 44 <targetConfig targets="lightning__RecordPage"> 45 <objects> 46 <object>cgcloud__Promotion__c</object> 47 </objects> 48 </targetConfig> 49 </targetConfigs> 50 <runtimeNamespace>cgcloud</runtimeNamespace> 51</LightningComponentBundle>
組織で Lightning Web セキュリティが有効である場合、メタデータファイルの runtimeNamespace プロパティは不要です。
メタデータファイルに runtimeNamespace プロパティを設定すると、コンポーネントが「@salesforce」パッケージ (ルートが @salesforce から始まる任意のインポート) からのモジュールを使用できなくなります。
トラブルシューティング
- 質問: コンポーネントを実行すると、[Scoped imports are not allowed when runtime namespace is specified (ランタイム名前空間が指定されているときは、範囲設定されたインポートは許可されません)] というエラーが表示されます。
回答: このエラーは、コンポーネントで「@salesforce」モジュールを使用しようとしたときに表示されます。メタデータファイルに runtimeNamespace が指定されると、コンポーネントは「@salesforce」リソースにアクセスできません。APEX コールなどこれらのモジュールが必要なアクションを実行するには、tpmGenericUtils モジュールの利用可能な関数を使用します。
- 質問: コンポーネントを実行すると、[Attempting to reference cross-namespace module (クロス名前空間モジュールを参照しようとしています)] というエラーが表示されます。エラーのシナリオ
- コンポーネントの runtimeNamespace プロパティがメタデータファイルで設定されていません。この問題を解決するには、名前空間が cgcloud の runtimeNamespace プロパティをコンポーネントに追加します。
- コンポーネントに含まれている他のカスタムコンポーネントの中に、runtimeNamespace プロパティがメタデータに指定されていないものがあります。名前空間が cgcloud の runtimeNamespace プロパティを、用意されているコンポーネントに追加します (このコンポーネントの用意されているコンポーネントにも追加します)。runtimeNamespace が定義された別のコンポーネントで使用されるすべてのコンポーネントは、そのメタデータに runtimeNamespace プロパティが指定されている必要があります。