Usage Of getRecord wire adapter

Let us discuss here how to use lightning/uiRecordApi module adaptors. This module includes wire adapters to record data and get default values to create records. It also includes JavaScript APIs to create, delete, update, and refresh records. Let’s discuss here how to use the getRecord wire adapter to get the Salesforce data without writing the apex class.

Syntax :

 

  • recordId—(Required) The ID of a record from a supported object.
  • fields—(Required) An array of fields to return. If the context user doesn’t have access to a field, an error is returned. If you’re not sure whether the context user has access to a field and you don’t want the request to fail if they don’t, use the optionalFields parameter.
  • optionalFields— (Optional) An array of optional field names. If a field is accessible to the context user, it’s included in the response. If a field isn’t accessible to the context user, it isn’t included in the response, but it doesn’t cause an error.
  • propertyOrFunction—A private property or function that receives the stream of data from the wire service. If a property is decorated with @wire, the results are returned to the property’s data property or error property. If a function is decorated with @wire, the results are returned in an object with a data property and an error property. For both the fields and optionalFields parameters, specify field names in the format ObjectApiName.FieldName or ObjectApiName.JunctionIdListName.

 

Error Handling

Use a Promise with then() and catch() blocks. You can display errors using toasts provided by the lightning/platformShowToastEvent module.

 

 

Create a lightning web component

create a Lightning web Component using this SFDX command

Use this code in recordex.html

use this code in recordex.js controller which will get the data from the Salesforce contact record and show on the UI.

Use this code in recordex.js-meta.xml

 

Push changes to scratch org

Push changes to scratch using this command and add this component to record page using lighting app builder

You can able to see the contact information as shown in below image.

 

Understand the code

The following code shows how to use the getRecord wire adapter to get the contact data based on the record id and we store the result in the contact property.

The below code shows how to get each field value from the contact property which contains a json array of data. This below code show how to refer the name field value and same get methods are defined for all field values in the code .

the below template syntax shows how to the call this method is the template.