getFieldValue(record, field)

レコードから項目の値を取得します。項目の拡張がサポートされています。

項目の値は、未加工のデータ形式で返されます。未加工のデータフォームは、getFieldDisplayValue(record, field) で返される表示値とは異なる場合があります。

構文 

1import { getFieldValue } from 'lightning/uiRecordApi';
2getFieldValue(record: Record, record: string)

パラメータ 

  • record — (必須) 項目値を取得するレコードオブジェクト。
  • field — (必須) 項目の API 名。値は文字列、または @salesforce/schema からインポートされた項目への参照にすることができます。最大 5 レベルの拡張項目を指定できます。たとえば、Opportunity.Account.CreatedBy.LastModifiedById は 4 レベルの拡張項目を返します。

戻り値 

項目の値。拡張項目が使用されている場合は、レコードが返される場合があります。要求している項目が存在しない場合、この関数は、undefined を返します。

使用方法 

レコードの項目値を取得するには、プロパティ record.data.fields.fieldName.value を返す getRecord ワイヤアダプタを使用します。ただし、getFieldValue(record, field) をコールして値を直接取得することもできます。

項目値は、計算や比較に便利な未加工のデータ形式で返されます。未加工のデータ形式の値の例を次にいくつか示します。

  • 通貨値は、350000000 のように表示されます。
  • 日付値は、2019-07-13 のように表示されます。
  • 日時値は、2015-06-17T22:17:58.000Z のように表示されます。

@salesforce/schema 範囲設定されたパッケージから getFieldValue(record, field) に渡す項目参照をインポートします。

1import { LightningElement, api, wire } from "lwc";
2import { getRecord, getFieldValue } from "lightning/uiRecordApi";
3
4import REVENUE_FIELD from "@salesforce/schema/Account.AnnualRevenue";
5import CREATED_FIELD from "@salesforce/schema/Account.CreatedDate";
6import EXP_FIELD from "@salesforce/schema/Account.SLAExpirationDate__c";
7
8const fields = [REVENUE_FIELD, CREATED_FIELD, EXP_FIELD];
9
10export default class WireGetValue extends LightningElement {
11  @api recordId;
12
13  @wire(getRecord, { recordId: "$recordId", fields })
14  account;
15
16  get revenue() {
17    return getFieldValue(this.account.data, REVENUE_FIELD);
18  }
19
20  get created() {
21    return getFieldValue(this.account.data, CREATED_FIELD);
22  }
23
24  get expiration() {
25    return getFieldValue(this.account.data, EXP_FIELD);
26  }
27}

書式設定された値を表示するには、lightning-formatted-numberlightning-formatted-date-time などの基本コンポーネントを使用します。Salesforce のユーザ設定に設定されているロケールによって書式が決まります。

1<template>
2  <lightning-card title="WireGetFieldValue" icon-name="standard:account">
3    <template lwc:if={account.data}>
4      <div class="slds-m-around_medium">
5        <p>
6          <lightning-formatted-number format-style="currency" value={revenue} currency-code="USD">
7          </lightning-formatted-number>
8        </p>
9        <p>
10          <lightning-formatted-date-time
11            weekday="long"
12            month="short"
13            year="numeric"
14            day="2-digit"
15            hour="2-digit"
16            minute="2-digit"
17            value={created}
18          >
19          </lightning-formatted-date-time>
20        </p>
21        <p>
22          <lightning-formatted-date-time value={expiration}> </lightning-formatted-date-time>
23        </p>
24      </div>
25    </template>
26  </lightning-card>
27</template>

基本コンポーネントには、次のような値が表示されます。

  • $350,000,000.00
  • Wednesday, Jun 17, 2015, 03:17 PM
  • Jul 13, 2014

または getFieldDisplayValue(record, field) ワイヤアダプタを使用して、項目の書式設定された値およびローカライズされた値を表示します。

The Japanese Summer '24 guide is now live

日本語の Summer '24 ガイドが公開されました! 「Component Reference (コンポーネントリファレンス)」は、以前と同様にコンポーネントライブラリにあります。