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. You can use relationship fields to traverse to parent objects and fields. You can specify up to three relationship fields, which results in four objects and the field being referenced. You can specify up to 5 levels of spanning fields. For example, Opportunity.Account.CreatedBy.LastModifiedById returns 4 levels of spanning fields.

Let’s look at the apexStaticSchema component from lwc-recipes. The component gets its data from an Apex method that returns a single contact.

Shows a contact, Amy Taylor, with title, VP of Engineering, and email address.

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