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";
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.
// SYNTAX
import SPANNING_FIELD_NAME from "@salesforce/schema/OBJECT.RELATIONSHIP.FIELD";
// EXAMPLE
import ACCOUNT_OWNER_NAME_FIELD from "@salesforce/schema/Account.Owner.Name";
import NAME_FIELD from "@salesforce/schema/Contact.Name";
import FIRSTNAME_FIELD from "@salesforce/schema/Contact.FirstName";
import LASTNAME_FIELD from "@salesforce/schema/Contact.LastName";
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 referenceimport { 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
Post a Comment