2

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
  • 1
    This is from the docs -- To return the Id, use the onsuccess handler. Are you facing issues using this?
    – Jayant Das
    Nov 19, 2020 at 15:10

1 Answer 1

5

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;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .