1

I am trying to retrieve the field value once a record is created and submit button is pressed. So I am getting the record Id via event.detail.id in my js but I tried retrieving the value with the Id but it is not working. In the result value, I am always getting the record id instead of the field value of the record. Can anyone highlight where I am going wrong? Here is my javascript and html. Thanks in advance!

Javascript:

import { LightningElement, api, track, wire } from "lwc";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import { NavigationMixin } from "lightning/navigation";
import {getRecord, getFieldValue} from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Invoice__c.Name';

export default class ContactRecord extends NavigationMixin(LightningElement) {
  @api recordId;


  @wire(getRecord, { recordId: 'a052v00000pulwaAAA', NAME_FIELD })
    invoice;

// eslint-disable-next-line no-unused-vars
  handleSubmit(event) {
    window.console.log("I am here");
    this.dispatchEvent(
      new ShowToastEvent({
        title: "Sucess!",
        message: "Successfully saved",
        variant: "success"
      })
    );
  }

    get name(){
    window.console.log(this.invoice.data);
    return getFieldValue(this.invoice.data, NAME_FIELD);
  }

  handleReset(event) {
    const inputFields = this.template.querySelectorAll("lightning-input-field");
    window.console.log("InputFields" + inputFields);
    if (inputFields) {
      inputFields.forEach(field => {
        field.reset();
      });
    }
  }

}

HTML

 <template>
  <div class="slds-p-bottom_large slds-p-left_large">
    <div class="hello">
      <lightning-record-edit-form
        object-api-name="Invoice__c"
        id="createInvoiceForm"
        onsubmit={handleSubmit}

      >
        <lightning-messages></lightning-messages>
        <lightning-input-field field-name="Name"></lightning-input-field>
        <lightning-input-field
          field-name="Total_Amount__c"
          required
        ></lightning-input-field>
        <lightning-input-field field-name="Password__c"></lightning-input-field>
        <lightning-button
          class="slds-p-right_x-small"
          type="submit"
          label="Submit"
        ></lightning-button>
        <lightning-button
          class="slds-m-top_small"
          label="Reset"
          onclick={handleReset}
        ></lightning-button>
      </lightning-record-edit-form>
    </div>
  </div>

  <lightning-record-view-form object-api-name="Invoice__c" record-id={recordId}>
    <div>
      <lightning-output-field field-name="Name"> </lightning-output-field>
      <lightning-output-field
        field-name="Total_Amount__c"
      ></lightning-output-field>
      <lightning-output-field field-name="Password__c"></lightning-output-field>
    </div>
  </lightning-record-view-form>

  <template if:true={invoice.data}>
    <div>
      <lightning-layout-item padding="around-small">
        <p>Name : {name}</p>
      </lightning-layout-item>
    </div>
  </template>



</template>
11
  • What do you wanna do? on successfull save fetch the password from database or UI? Commented Mar 23, 2020 at 8:50
  • Also onsuccess only gives you recordId not actual record? Commented Mar 23, 2020 at 8:51
  • @PranayJaiswal On successful save I want to display the field values that was just created. Commented Mar 23, 2020 at 8:59
  • @PranayJaiswal Any idea on how I can get the field values from the record id? Commented Mar 23, 2020 at 9:00
  • getRecord to get latest value from db? Commented Mar 23, 2020 at 9:01

2 Answers 2

1

In this line:

const result = getFieldValue(record, PASSWORD_FIELD);

If you want to make getFieldValue function work - you have to pass record object instead of record Id:

Wire Adapter Parameters:

record—(Required) A Record object from which to retrieve the field value.

field—(Required) The API name of the field.

See here: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_get_field_value

3
  • Hey valentine, OP does not have a record. He is using lightning-record-edit-form Commented Mar 23, 2020 at 10:13
  • Hi Jaiswal, I see, I just added this comment in scope of getFieldValue function which will not work this way Commented Mar 23, 2020 at 10:22
  • @ValentinePotapov Please check my code. I have made edits but still no luck. Commented Mar 23, 2020 at 10:34
0

It's probably because the getter executes after the wire fired but before it's callback executes providing the data and populating this.invoice. Which is the reason why all the examples in documentation do the check

return this.invoice.data ? getSObjectValue(this.invoice.data, NAME_FIELD) : '';

You must log in to answer this question.

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