Sub topic 3
Importing fields from @salesforce/schema
This chapter assumes you know JavaScript modules, the structure of a Lightning web component, and the difference between an object and a field in Salesforce. You will learn how to import field references instead of typing API names as strings, and what those references give you.
Why import instead of typing strings
You could pass 'Account.Name' as a plain string to getRecord. It works, but nothing checks it. A typo only shows up at runtime, and a deleted field only fails after deployment.
Importing from @salesforce/schema fixes this. The platform resolves the import when the component is compiled or deployed. It also records that your component depends on the field.
- A misspelled or missing field produces an error when you save or deploy the component. It does not produce a silent failure in the browser.
- Salesforce blocks deletion of a field that a component references.
- The import is a reference to the field. It is not a copy of its value.
The import syntax
The path has the form @salesforce/schema/Object.Field. The default export of that path is a reference object.
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';Custom objects and fields keep their __c suffix, for example @salesforce/schema/Invoice__c.Amount__c. For fields in a managed package, include the namespace prefix in the name.
Imported names are constants, so the usual convention is upper case with a _FIELD or _OBJECT suffix.
What the reference contains
Each import is an object with two properties.
| Property | Meaning | Example for `Account.Name` |
|---|---|---|
| `fieldApiName` | The field's API name without the object | `Name` |
| `objectApiName` | The object's API name | `Account` |
An object import such as ACCOUNT_OBJECT exposes objectApiName. You can check this by logging the references.
import { LightningElement } from 'lwc';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class SchemaInspector extends LightningElement {
connectedCallback() {
console.log(ACCOUNT_OBJECT.objectApiName); // Account
console.log(NAME_FIELD.fieldApiName); // Name
console.log(NAME_FIELD.objectApiName); // Account
}
}Because fieldApiName is a property, you can build the field-name keys that updateRecord expects without hard-coding them. A later chapter uses fields[NAME_FIELD.fieldApiName] = value.
Where the references are used
The same reference works in several places.
- In the
fieldsandoptionalFieldsarrays of agetRecordwire. - As the second argument of
getFieldValueandgetFieldDisplayValue, which pick a value out of a result. - As keys for
updateRecordthroughfieldApiName.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class AccountName extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
account;
get name() {
return getFieldValue(this.account.data, NAME_FIELD);
}
}The same NAME_FIELD constant requests the field and reads it back. You write the field name once, in the import.
Fields on related objects
To reach a field through a lookup, import the path with the relationship name. @salesforce/schema/Account.Owner.Name refers to the name of the account's owner.
import OWNER_NAME_FIELD from '@salesforce/schema/Account.Owner.Name';Use it like any other reference. getFieldValue(this.account.data, OWNER_NAME_FIELD) reads the nested value.
Common errors
- Importing a field the user cannot see. The import resolves, but the record data will not include the field. Access is checked when the data is requested.
- Using the label. Use the API name, such as
Annual_Revenue__c, and not the label "Annual Revenue". - Importing the value. The import is a reference. Pass it to an adapter or helper, and do not display it directly.
- Wrong object in the path.
@salesforce/schema/Contact.Nameis a different field fromAccount.Name. A record of one type does not return fields of the other.
Exercise
A component imports NAME_FIELD from @salesforce/schema/Account.Name. Which value does NAME_FIELD.fieldApiName return, and why is it useful when building an updateRecord input?
Show answer
It returns Name, the field's API name without the object. It lets you set the key as fields[NAME_FIELD.fieldApiName] so the field name is never typed as a string, and a rename or typo is caught at deployment.
Exercise
Why is importing a field from @salesforce/schema safer than passing the string 'Account.Name' to getRecord?
Show answer
The platform validates the import at compile or deploy time, so a misspelled or missing field is reported as an error. It also tracks the dependency, which prevents deleting a field that a component uses.