Import Objects and Fields from @salesforce/schema
If you import references to objects and fields, Salesforce verifies that the objects and fields exist (which also catches typos), prevents objects and fields from being deleted, and cascades any renamed objects and fields into your component's source code. It also ensures that dependent objects and fields are included in change sets and packages. Importing references to objects and fields ensures that your code works, even when object and field names change.
If a component gets an object via an Apex class and imports references to the object from @salesforce/schema
, call getSObjectValue()
to get a field value from the object.
{sobject}
—The object returned by the Apex method.{fieldApiName}
—The API name of the field. The value can be either a string or reference to a field imported from@salesforce/schema
.- In your
fieldApiName
input, you can specify up to three relationship fields to reference parent objects and fields. Follow this format:<SObjectName>.<relationship-1>.<relationship-2>.<relationship-3>.<fieldName>
.
- In your
Let’s look at the apexStaticSchema
component from lwc-recipes. The component gets its data from an Apex method that returns a single contact.
The JavaScript imports the getSObjectValue
function and the getSingleContact
method from @salesforce/apex
. It imports references to contact fields from @salesforce/schema
.
The Apex method is wired to the contact
property. The name
, title
, and email
properties in the template have getters that call getSObjectValue()
to extract the value from the specified field.
See Also