Leveraging Object and Field References in LWC Wire Adapters


When utilizing a wire adapter, it is strongly advised to import references to fields and objects in a lightning/ui*Api module. Salesforce verifies the validity of the fields and objects, prevents their removal, and propagates any rename modifications into your component's source code. It also ensures that dependent objects and fields are included in change sets and packages. If references to objects and fields are imported, your code will still work even if their names change in salesforce. 

Lightning Web Components supports the import of references to standard objects, as well as the import of references to custom objects (__c) only.

To import a reference to an object, use this syntax.

import objectName from "@salesforce/schema/object"; import objectName from "@salesforce/schema/namespace__object"; import FIELD_NAME from "@salesforce/schema/object.field";

Example:

import ACCOUNT_OBJECT from "@salesforce/schema/Account"; import ACCOUNT_NAME_FIELD from "@salesforce/schema/Account.Name";

// CUSTOM OBJECT import MY_CUSTOM_OBJ_OBJECT from "@salesforce/schema/My_Custom_Obj__c";

Relationship fields
Use syntax below to import a reference to a field via a relationship. Relationship fields are useful for navigating to parent fields and objects. There are four objects and the field being referenced when you specify up to three relationship fields.

For example, Opportunity.Account.CreatedBy.LastModifiedById returns 4 levels of spanning fields.

// SYNTAX import SPANNING_FIELD_NAME from "@salesforce/schema/OBJECT.RELATIONSHIP.FIELD"; // EXAMPLE import ACCOUNT_OWNER_NAME_FIELD from "@salesforce/schema/Account.Owner.Name";

Compound fields
Name fields, address fields, and geolocation fields are compound fields. A compound field is accessible as a single, structured field, or as individual constituent fields.

You can import a reference to a compound field or to the fields that make up the compound in a read operation. For instance, you can use the compound field Contact.Name in a read operation.

import NAME_FIELD from "@salesforce/schema/Contact.Name";

You must import the constituent fields in order to create and update operations on compound fields using lightning/ui*Api functions like updateRecord(recordInput, clientOptions).

Don't forget to fill out every constituent field that is necessary. For instance, the LastName column must be filled up in order to create a contact.

import FIRSTNAME_FIELD from "@salesforce/schema/Contact.FirstName"; import LASTNAME_FIELD from "@salesforce/schema/Contact.LastName";

Full code example without reference

import { LightningElement, api, wire } from "lwc"; import { getRecord } from "lightning/uiRecordApi"; export default class Record extends LightningElement { @api recordId; @wire(getRecord, { recordId: "$recordId", fields: ["Account.Name"] }) record; }

Full code example with reference

import { LightningElement, api, wire } from "lwc"; import { getRecord } from "lightning/uiRecordApi"; import ACCOUNT_NAME_FIELD from "@salesforce/schema/Account.Name"; export default class Record extends LightningElement { @api recordId; @wire(getRecord, { recordId: "$recordId", fields: [ACCOUNT_NAME_FIELD] }) record; }

Comments

Popular posts from this blog

How to Add a Dynamic Child List to Parent in Apex Class || Trigger using map

A - Z Guide to Using the Wire Decorator in LWC Salesforce

(Salesforce Apex 2024 Release) getSalesforceBaseUrl method is deprecated