How do i get the record ID of the newly created record when i use this.template.querySelector('lightning-record-edit-form').submit()
and store it in a variable?
1 Answer
According to docs Returning the Record Id
LWC documentation, record ID is returned via onsuccess
handler. Example given below same as doc.
<template>
<lightning-record-edit-form object-api-name="Account" onsuccess={handleSuccess}>
<lightning-messages></lightning-messages>
<div class="slds-m-around_medium">
<lightning-input-field field-name='Id' value={accountIdToBeShown}></lightning-input-field>
<lightning-input-field field-name='Name'></lightning-input-field>
<div class="slds-m-top_medium">
<lightning-button variant="brand" type="submit" name="save" label="Create Account">
</lightning-button>
</div>
</div>
</lightning-record-edit-form>
</template>
JS:
export default class CustomSearchSobjectLwc extends LightningElement {
accountIdToBeShown;
handleSuccess(event) {
console.log(event.detail.id);
this.accountIdToBeShown = event.detail.id;
}
}
onsuccess
handler. Are you facing issues using this?