tpmGenericUtils モジュールリファレンス
このコンポーネントでは、runtimeNamespace cgcloud が定義されているコンポーネントの @salesforce モジュールにアクセスするための代替手段が提供されます。
名前空間
1cgcloud実装例
1import {
2 apex,
3 apexCacheable,
4 getResponse,
5 getStaticResourceURL,
6 i18n,
7 UserId,
8 refreshApex,
9 getSObjectValue,
10 FORM_FACTOR,
11 CGCloudNamespace,
12 CGCloudNamespaceWithUnderscore
13} from 'cgcloud/tpmGenericUtils';APEX コールの実行
このモジュールには、apex メソッドをコールするときに使用する apex および apexCacheable 関数があります。最初に、コールする APEX メソッドを定義しましょう。コールする APEX メソッドは、System.Callable インターフェースを実装している global class にする必要があります。
1global with sharing class ApexTest implements System.Callable {
2 global class ApexTestException extends Exception {}
3
4 // Interface method
5 public Object call(String method, Map<String, Object> params) {
6 // Both method and params will be received
7 if (method == 'getAccounts') {
8 return getAccounts(params);
9 } else {
10 // Exceptions can be thrown
11 throw new Exception('Invalid method!');
12 }
13 }
14
15 private static List<Account> getAccounts(Map<String, Object> params) {
16 // Validate parameters
17 if (params == null || String.isBlank((String) params.get('searchKey')) == null) {
18 return new List<Account>();
19 }
20
21 // Extract parameters.
22 // NOTE: Javascript Dates are received as Strings, they need to be parsed
23 // with
24 // Date newDate = Date.valueOf((String.valueOf(dateString)));
25 String searchKey = String.valueOf(params.get('searchKey'));
26 String searchKeyProcessed = '%' + String.escapeSingleQuotes(searchKey) + '%';
27
28 // return
29 return [SELECT Id, Name FROM Account WHERE Name LIKE :searchKeyProcessed LIMIT 100];
30 }
31}カスタム LWC では、apex および「apexCacheable」メソッドを使用して、この関数をコールできます。apex と apexCacheable の違いとして、apexCacheable では、Lightning Web コンポーネントのキャッシュレイヤーが使用されます。
1import { api, wire, LightningElement } from 'lwc';
2
3import {
4 CGCloudNamespaceWithUnderscore,
5 apexCacheable,
6 apex,
7 getResponse,
8 refreshApex
9} from 'cgcloud/tpmGenericUtils';
10
11export default class MyApexTestComponent extends LightningElement {
12
13 imperativeData = null;
14
15 wiredResponse = null;
16 wiredData = null;
17
18 // Store the params for the request
19 requestParams = {
20 searchKey = ''
21 };
22
23 // It's possible to use the wire adapter only with the apexCacheable method
24 @wire(apexCacheable, {
25 descriptor: {
26 className: 'ApexTest',
27 method: 'getAccounts'
28 },
29 params: '$requestParams'
30 })
31 wiredFunction(value) {
32 this.wiredResponse = value; // Save wired data to be able to call
33 // refreshApex later
34 const { data, error } = getResponse(value); // Use 'getResponse' to extract data
35 if (error) {
36 console.error(error);
37 }
38 this.wiredData = data;
39 }
40
41 onSearchKeyChange(event) {
42 // Set the new parameters
43 this.requestParams = {
44 searchKey = event.detail.value
45 };
46 // The change to requestParams will trigger a refresh of
47 // the wire adapter data.
48 }
49
50 onOtherEvent() {
51 // You can force-refresh the wire adapter data
52 // with refreshApex
53 refreshApex(this.wiredResponse);
54 }
55
56 // You can call APEX method imperatively
57 imperativeApex() {
58 apex({ // You can also use apexCacheable to cache the response
59 descriptor: {
60 className: 'ApexTest',
61 method: 'getAccounts'
62 },
63 params: this.requestParams
64 })
65 .then(getResponse) // use getResponse to extract data
66 .then((data) => {
67 this.imperativeData = data;
68 });
69 }
70}静的リソースへの読み込み
静的リソースを読み込むには、getStaticResourceURL 関数を使用して静的リソースの URL を取得し、Lightning Web コンポーネントの標準ツールを使用して、リソースを読み込みます。
1import { LightningElement } from 'lwc';
2import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
3import {
4 getStaticResourceURL
5} from 'cgcloud/tpmGenericUtils';
6
7// getStaticResourceURL returns a Promise
8const myZippedResourceURLPromise = getStaticResourceURL('MyZippedResource');
9
10export default class MyStaticResourceComponent extends LightningElement {
11
12 connectedCallback() {
13 // Wait for the Base URL promise to resolve
14 myZippedResourceURLPromise
15 .then((baseURL) => Promise.all([
16 // load your resources
17 loadScript(this, baseURL + '/MyScript.js'),
18 loadStyle(this, baseURL + '/MyStyle.css')
19 ]))
20 .then(() => {
21 // You can use your resource after loaded
22 window.MyApp.run();
23 });
24 }
25}Internationalization プロパティへのアクセス
i18n オブジェクトを使用して Internationalization プロパティを取得できます。公開されているプロパティは、「Salesforce Developer Component Library Documentation (Salesforce 開発者コンポーネントライブラリドキュメント)」に記載されているプロパティです。
1import {
2 i18n
3} from 'cgcloud/tpmGenericUtils';
4
5console.log(i18n);
6
7// {
8// "lang": "en-US",
9// "dir": "ltr",
10// "locale": "en-US",
11// "currency": "USD",
12// "firstDayOfWeek": 1,
13// "datetime": {
14// "shortDateFormat": "M/d/yyyy",
15// "mediumDateFormat": "MMM d, yyyy",
16// "longDateFormat": "MMMM d, yyyy",
17// "shortDateTimeFormat": "M/d/yyyy, h:mm a",
18// "mediumDateTimeFormat": "MMM d, yyyy, h:mm:ss a",
19// "shortTimeFormat": "h:mm a"
20// },
21// "number": {
22// "currencyFormat": "¤#,##0.00",
23// "currencySymbol": "$",
24// "decimalSeparator": ".",
25// "groupingSeparator": ",",
26// "numberFormat": "#,##0.###",
27// "percentFormat": "#,##0%"
28// },
29// "timeZone": "America/Los_Angeles"
30// }現在のユーザ ID の取得
現在のユーザ ID は、UserId 属性で返されます。
1import {
2 UserId
3} from 'cgcloud/tpmGenericUtils';
4
5console.log(UserId);
6
7// 005TC0000002jXJYAYクライアントのフォーム要素の取得
クライアントのフォーム要素は、FORM_FACTOR 属性で返されます。
1import {
2 FORM_FACTOR
3} from 'cgcloud/tpmGenericUtils';
4
5console.log(FORM_FACTOR);
6
7// LargeCGCloud 名前空間の取得
CGCloud 名前空間は、CGCloudNamespace および CGCloudNamespaceWithUnderscore プロパティで取得できます。
1import {
2 CGCloudNamespace,
3 CGCloudNamespaceWithUnderscore
4} from 'cgcloud/tpmGenericUtils';
5
6console.log(CGCloudNamespace);
7
8// cgcloud
9
10console.log(CGCloudNamespaceWithUnderscore);
11
12// cgcloud__