<aside>
π Please review this page recordId and
objectApiName special properties
and Accessing Data with LWC, which are the basics for understanding this LWC.
</aside>
The main concept included in this recipe is creating a record by using several wire adapters from the lightning/uiRecordApi
module.
<aside> π‘ I strongly recommend that you follow the official documentation, starting at this page while reading this.
</aside>
Before you look at the code, I also recommend changing a value or the Account/AreaNumber
field, so this will help you understand the goal of this LWC:
Area Number
field, from 1000 to 555, as shown in the image bellow.<aside>
π You can find this LWC in this relative path: force-app\\main\\default\\lwc\\ldsCreateRecord\\ldsGenerateRecordInputForCreate
</aside>
The template uses the initialisations carried in the Javascript file:
recordInput
is not empty, then 2 fields and a button are displayed.Area Number
field is only displayed if the areaNumberCreateable
is true
, which is set in the initialization of the JavaScript file....
<template lwc:if={recordInput}>
<lightning-input
label="Name"
onchange={handleFieldChange}
data-field-name={nameField}
></lightning-input>
<template lwc:if={areaNumberCreateable}>
<lightning-input
type="number"
label="Area Number"
onchange={handleFieldChange}
data-field-name={areaNumberField}
value={areaNumber}
></lightning-input>
</template>
<div class="slds-var-m-top_x-small">
<lightning-button
label="Create Account"
variant="brand"
onclick={createAccount}
></lightning-button>
</div>
</template>
<template lwc:elseif={error}>
<c-error-panel errors={error}></c-error-panel>
</template>
...
As the goal in this recipe is creating an Account record, so for this we should use the createRecord
wire adapter. This adapter requires a recordInput
parameter which type is RecordInput.
This parameter can be created manually as we did in the previous recipe: LWC LdsCreateRecord but this is not ideal, as you miss important information as the default record values and the accessibility related to the fields (are all of them creatable?).
So, the preferred option is to use another wire adapter, the generateRecordInputForCreate(record, objectInfo)
:
generateRecordInputForCreate(record, objectInfo)
generates a RecordInput
object and optionally only retrieves the fields that are creatable. This is crucial to avoid issues in run-time.record
parameter required by the generateRecordInputForCreate
requires calling another wire adapter: getRecordCreateDefaults
Β orΒ getRecord
.