Sub topic 3
Importing fields from @salesforce/schema
This chapter assumes you can write a Lightning web component with an HTML template, a JavaScript class, and a metadata file. It also assumes you know ES module imports, and that you know Salesforce objects, fields, and API names. You will learn how the @salesforce/schema import path works, what each import gives you, and where it goes wrong.
Why import instead of typing a string
You can ask getRecord for fields with plain strings such as 'Account.Name'. That works, but a typo in the string only shows up at run time, when the wire returns an error.
A schema import moves that check earlier. The platform resolves the import when the component is saved or deployed. If the field does not exist, the deployment fails with a message that names the missing reference.
Schema imports also record a dependency. Salesforce knows your component uses Account.Industry, so it blocks deletion of that field until you remove the reference. A string in your code creates no such dependency, so deleting the field leaves a component that fails at run time.
| Concern | String such as `'Account.Name'` | Schema import |
|---|---|---|
| Typo detection | At run time, through a wire error | When the component is saved or deployed |
| Blocks deletion of the field | No | Yes |
| Handles namespaces | You must write the prefix yourself | The path carries the prefix |
| Editor and reader clarity | Anonymous string | Named constant |
The import syntax
A schema import is a default import from a path under @salesforce/schema. The path is ObjectApiName for an object, or ObjectApiName.FieldApiName for a field.
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';The name on the left is yours to choose. By convention, constants are upper case and end in _OBJECT or _FIELD.
Custom objects and fields use their full API names, including the __c suffix. A managed package adds its namespace prefix to both parts of the path.
import BUDGET_FIELD from '@salesforce/schema/Project__c.Budget__c';
import PACKAGED_FIELD from '@salesforce/schema/acme__Invoice__c.acme__Total__c';You can also import a field reached through a relationship. The path continues through the relationship name.
import OWNER_NAME_FIELD from '@salesforce/schema/Account.Owner.Name';Use this form when you want a value from a parent record, such as the account owner's name, without a second request.
What the import gives you
A schema import is an object, not a string. An object import has an objectApiName property. A field import has a fieldApiName property and an objectApiName property.
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';
console.log(ACCOUNT_OBJECT.objectApiName); // "Account"
console.log(NAME_FIELD.fieldApiName); // "Name"
console.log(NAME_FIELD.objectApiName); // "Account"You rarely read these properties directly. Most of the time you pass the whole import to an API that knows how to use it. The exception is building a record input for updateRecord, where you need the bare field name as an object key. The later chapter on saving changes covers that case.
The diagram below shows where the import is used.
Using imports in a component
A field reference goes into the fields or optionalFields array of getRecord. The same reference then goes into getFieldValue to read the result. You use one constant in both places, so the request and the read cannot drift apart.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import OWNER_NAME_FIELD from '@salesforce/schema/Account.Owner.Name';
export default class AccountOwner extends LightningElement {
@api recordId;
@wire(getRecord, {
recordId: '$recordId',
fields: [NAME_FIELD, OWNER_NAME_FIELD]
})
account;
get name() {
return getFieldValue(this.account.data, NAME_FIELD);
}
get ownerName() {
return getFieldValue(this.account.data, OWNER_NAME_FIELD);
}
}<template>
<template lwc:if={account.data}>
<p>Account: {name}</p>
<p>Owner: {ownerName}</p>
</template>
</template>Later chapters cover the wire, the $ prefix, and the helper functions in detail. Here, notice only that each field is imported once and used twice.
An object import is useful when an API asks for an object API name. For example, the lightning-record-form base component takes object-api-name. In JavaScript you can expose the value to the template through a getter.
import { LightningElement } from 'lwc';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
export default class AccountObjectName extends LightningElement {
get objectApiName() {
return ACCOUNT_OBJECT.objectApiName;
}
}Rules and limits
Schema imports follow a few rules that explain most of the errors people hit.
- The path is static. You cannot build it from a variable, because the platform reads the import before any code runs.
import X from '@salesforce/schema/' + nameis a syntax error. - The import must be a default import.
import { NAME_FIELD } from ...does not work. - The path must match an object or field that exists in the org where the component is deployed. A component that imports
Project__c.Budget__cfails to deploy to an org without that field. - The import gives you a reference, not a value. It tells you nothing about what the field holds on any record.
Because the path is static, a component that must work with any object chosen at run time cannot use schema imports for those fields. In that case you build field names as strings, or use a different API. Strings in the Object.Field form are accepted by getRecord, but they lose the deploy-time check.
Common errors
A "does not exist" error on deploy. The path has a typo, the field has a different API name than its label, or the field is missing from the target org. Check the API name in Object Manager, not the label shown on the page layout.
A missing namespace prefix. For a managed package field, the object and the field both carry the prefix. Writing acme__Invoice__c.Total__c fails.
A named import. Writing import { NAME_FIELD } from '@salesforce/schema/Account.Name' leaves NAME_FIELD undefined, so the wire is configured with an undefined field. Always use a default import.
Reading the import as a string. Code such as 'Account.Name' === NAME_FIELD is false, because NAME_FIELD is an object. Compare fieldApiName values if you need to compare names.
Mixing up the Id field. To save a record you need the Id field in the update input. Import it as @salesforce/schema/Account.Id, the same as any other field.
Summary
- A schema import is a static default import from
@salesforce/schema/Objector@salesforce/schema/Object.Field. - The platform validates the path when the component is saved or deployed, and it blocks deleting a field that a component references.
- A field import is an object with
fieldApiNameandobjectApiName. You pass the whole object togetRecord,getFieldValue, andgetFieldDisplayValue. - Dynamic field names cannot use schema imports. Use strings and accept the loss of deploy-time checks.
Exercise
A component imports @salesforce/schema/Account.Industry as INDUSTRY_FIELD. A teammate wants to remove the Industry field from the org and says the code is safe because it never types the string "Industry". What happens when they try, and what is INDUSTRY_FIELD.fieldApiName?
Show answer
Salesforce blocks the deletion, because the schema import records a dependency from the component to the field. The teammate must remove the import and its uses first. INDUSTRY_FIELD.fieldApiName is the string "Industry".
Exercise
Why does import { NAME_FIELD } from '@salesforce/schema/Account.Name'; break a wire that lists fields: [NAME_FIELD], and how do you fix it?
Show answer
Schema paths provide only a default export, so the named import leaves NAME_FIELD undefined and the wire is configured with an invalid field. Fix it with a default import: import NAME_FIELD from '@salesforce/schema/Account.Name';.