Issue pulling field value from record-edit-form

lightning-web-components

I am trying to get field values onLoad from a LWC record-edit-form. From what I read here I should be able to pull the field value using the below:

<lightning-record-edit-form
    object-api-name="Account"
    record-id={recordId}
    onload={getPhoneNumber}
>
    <lightning-input-field field-name="Account_Phone_s_Country__c" onchange={onPhoneCountryChange}> </lightning-input-field>
    <lightning-input-field field-name="Phone" value={phoneNumber}> </lightning-input-field>
    <div class="slds-var-m-top_medium">
        <lightning-button variant="brand" type="submit" label="Save">
        </lightning-button>
    </div>
</lightning-record-edit-form>

JS:

getPhoneNumber(event) {
    let record = event.detail.records;
    let fields = record[this.recordId].fields;
    
    this.phoneNumber = fields.Phone.value;
    if(fields.Account_Phone_s_Country__c) this.previousCCValue = fields.Account_Phone_s_Country__c.value;
}

This does not work. Somehow, fields.Account_Phone_s_Country__c is both undefined (per debugging in Chrome with a breakpoint on the if) and passes this check. I can also explicitly check that it != (or !==) undefined and it still runs. When it does, it throws an error because the actual object is actually undefined.

I can get around this with wire but it seems like I shouldn't need to given that they're already being retrieved for the edit form. Does anyone know how to get this working?

Best Answer

I know the question is old and it might be late for the OP now, but the reason the field is not getting returned is because it is not on the page layout of the record type that you are accessing with lightning-record-edit-form.

Just had this same issue and adding the field to the page layout solved it. Hope this helps.