Get default information and data needed to create a record in Lightning Web Component | getRecordCreateDefaults | LWC Stack ☁️⚡️


In this blog you will learn how to get default information and data needed to create a record in Lightning Web Component.

For that we will be using getRecordCreateDefaults uiRecordApi

To check the official documentation you may follow this link https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_wire_adapters_create_record_values


Parameters

  • objectApiName— (Required) A supported object.
  • formFactor— (Optional) The layout display size for the record. An array containing any of these values:
    • Large—(Default) Use this value to get a layout for desktop display size.
    • Medium—Use this value to get a layout for tablet display size.
    • Small—Use this value to get a layout for phone display size.

To get a value to pass as the formFactor, use the @salesforce/client/formFactor module.

  • recordTypeId— (Optional)The ID of the record type (RecordType object) for the new record. If not provided, the default record type is used.
  • optionalFields— (Optional) An array of fields to return along with the default fields. If an optional field is accessible to the context user, it’s included in the response. If it 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.

Returns
  • data—Record Defaults 
    In the response, don’t use the recordTypeInfo property. Instead, use the recordTypeId property, which is returned for every record.
  • error—FetchResponse
Usage
To create UI that lets a user create a record, first get information about which fields are required. This adapter’s response contains the default field values for a new record of the object type specified in {apiName}. It also contains object metadata and the corresponding layout for Create mode. In the Salesforce user interface, an admin with “Customize Application” permission can mark a field as required in a layout. When you’re building UI, to determine which fields to mark as required in a layout for create and update, use the ObjectInfo.fields[fieldName].required property.


Sample Code

HTML
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<template>
  <div class="slds-box slds-theme_default">
    <table
      class="
        slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped
      "
    >
      <thead>
        <tr>
          <th>Name</th>
          <th>Data Type</th>
          <th>Required ?</th>
        </tr>
      </thead>
      <tbody>
        <template for:each={createDefaultsData} for:item="data">
          <tr key={data.apiName}>
            <td>{data.label}</td>
            <td>{data.dataType}</td>
            <td>{data.required}</td>
          </tr>
        </template>
      </tbody>
    </table>
  </div></template
>


JS
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import { LightningElement, wire } from "lwc";
import { getRecordCreateDefaults } from "lightning/uiRecordApi";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";

export default class GetDefaultInformation extends LightningElement {
  createDefaultsData = [];
  @wire(getRecordCreateDefaults, { objectApiName: ACCOUNT_OBJECT })
  wiredRecordDefaults({ data, error }) {
    if (data) {
      console.log("###Data : " + JSON.stringify(data));
      const { fields } = data.objectInfos.Account;
      this.createDefaultsData = Object.keys(fields).map((key) => {
        let field = fields[key];
        const { apiName, label, dataType, required } = field;
        return { apiName, label, dataType, required };
      });
    } else if (error) {
      console.error("###Error : " + JSON.stringify(error));
    }
  }
}


Output



Checkout complete video tutorial below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments