Tags

,

Let’s say we are using lightning-record-edit form in LWC to display/edit the data and we have a requirement to get the field values during the onLoad event of the form.

For instance, onLoad event calls handleOnLoad method and the code below shows the right approach to get the field values from the form based on the recordId:

handleOnLoad(event) {
        var record = event.detail.records;
        var fields = record[this.recordId].fields;(obviously we need to have the recordId declared as @api decorator and the lwc is placed on the record page. Please refer the link https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_record_context to read more about it)
        const assetName = fields.Name.value;      
        alert(assetName);
}

We can use template.querySelector as well(as given below), but it is not the right approach(rather not a good approach):

handleOnLoad() {
    let name = this.template.querySelector('.name').value;//in the html, we should have given class="name" for the name field
}

Hope this post is helpful and let me know for any questions. Thanks!!