0

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?

4 Answers 4

1

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.

0

All you have to do is understand the json structure of event.detail.records in onload event payload for lightning-record-edit-form.

i.e. Map<String, Record>

{
  "001B000001Off4GIAR": {
    "apiName": "Account",
    "childRelationships": {
      
    },
    "fields": {
      "AccountNumber": {
        "displayValue": null,
        "value": null
      },
      "Active__c": {
        "displayValue": null,
        "value": null
      },
      ..... other fields1 ----,
      ..... other fields2 ....
    },
    "id": "001B000001Off4GIAR",
    "lastModifiedById": "005B0000007bnNmIAI",
    "lastModifiedDate": "2022-01-30T05:43:38.000Z",
    "recordTypeId": "012000000000000AAA",
    "recordTypeInfo": null,
    "systemModstamp": "2022-01-30T05:43:38.000Z"
  }
}

According to record-edit-form documentation, Use the event.detail property to return the record UI, and picklist values if you include picklist fields in the form.

So to get the value, you should use below piece of code:-

import { LightningElement, api } from 'lwc';

export default class RecordFormLwc extends LightningElement {
    @api recordId;
    handleOnLoad(event) {
        console.log(JSON.stringify(event.detail.records[this.recordId].fields.Phone));
    }
}

The value in the console will be like this:-

{"displayValue":null,"value":"12345676890"}

If you want to get the exact value -

console.log(JSON.stringify(event.detail.records[this.recordId].fields.Phone.value));
1
  • As I said, this isn't returning a value. Thats why I created the Q.
    – SFD
    Feb 8, 2022 at 5:19
0

event.detail.records[this.recordId]. <- Here, I think you should pass index of the record in [], not Id. Use findIndex :)

3
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
    – identigral
    Jun 22, 2022 at 15:00
  • Hey🙃 I don't want to waste my time for solving other people's problem for gaining reputation. I just need help, willing to provide help, but won't spend whole time to solve the problem that's not my own, so was briefly commented what I thought. Yesterday I helped teammates solving similar issue, so thought the concept of understanding structure and using proper JS method might help. Even though I hadn't understood the question well, it's the questioner's choice who decide the answer. Why don't you provide the solution to the question? I want to learn 😄.
    – 고수군
    Jun 23, 2022 at 15:27
  • This answer actually seems useful, but would be better if you were to edit to include what you mean about findIndex.
    – Adrian Larson
    Jun 25, 2022 at 23:36
0

You can get the field value by:

handleFormOnLoad(event){
    Let fieldValue = event.detail.records[this.recordId].fields["Field_API_Name__c"].value;
}

You must log in to answer this question.

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