0

I have an LWC component and I'm trying to add a new record using record-edit-form:

                  <lightning-record-edit-form
                      object-api-name={objectApiNameExpenseCard}
                      onsuccess={handleSuccessNewExpense}>
                        <lightning-layout>
                            <lightning-input-field field-name={AMOUNT}>
                            </lightning-input-field>
                        </lightning-layout>
                        <lightning-layout>
                            <lightning-input-field field-name={DATE}>
                            </lightning-input-field>
                        </lightning-layout>
                        <lightning-layout>
                            <lightning-input-field field-name={DESCRIPTION}>
                            </lightning-input-field>
                        </lightning-layout>
                        <lightning-button
                            class="slds-float_right"
                            variant="brand"
                            type="submit"
                            label="Save"
                            onclick={handleSaveNewExpense}>
                        </lightning-button>
                        <lightning-button
                            class="slds-float_right"
                            type="submit"
                            label="Cancel"
                            onclick={handleReset}>
                        </lightning-button>
                    </lightning-record-edit-form>

The fact is that before sending, I need to add another field (CardKeeper__c), which does not need to be displayed on the form. But I don't understand how to do this? Here is my js code:

import { LightningElement, track, api } from 'lwc';
import EXPENSE_CARD_OBJECT from '@salesforce/schema/Expense_Card__c';
import AMOUNT from '@salesforce/schema/Expense_Card__c.Amount__c';
import DATE from '@salesforce/schema/Expense_Card__c.CardDate__c';
import DESCRIPTION from '@salesforce/schema/Expense_Card__c.Description__c';

export default class ExpensesPage extends LightningElement {
    @api currentContact;
    @api objectApiNameExpenseCard = EXPENSE_CARD_OBJECT;
    AMOUNT = AMOUNT;
    DATE = DATE;
    DESCRIPTION = DESCRIPTION;

    handleSaveNewExpense(event){
        event.preventDefault();
        fields = event.detail.fields;
        fields.CardKeeper__c = this.currentContact.Id;
        this.template.querySelector('lightning-record-edit-form').submit(fields);
    }
}
7
  • are you getting any error or what is currently happening when you submit? Jul 7 at 16:51
  • Everything is fine with the shipment. The field that is not present on the form and that I am trying to add before sending is not added (CardKeeper__c). Jul 7 at 17:07
  • what if you try fields["CardKeeper__c"] = this.currentContact.Id;? Jul 7 at 17:29
  • Does the user have write permission on this field?
    – Phil W
    Jul 7 at 20:15
  • 1
    @Kris I tried it, it doesn't work... I don't even understand why when trying to output the contents of the fields const fields = event.detail.fields;I get undefined... Jul 7 at 22:17
1

You're using the click event from your submit button instead of the submit event provided by lightning-record-edit-form. This is why when you try event.detail.fields, it's undefined as it's not the right event and fields is not being provided by the component.

<lightning-record-edit-form
    object-api-name={objectApiNameExpenseCard}
    onsuccess={handleSuccessNewExpense}>
    onsubmit={handleSaveNewExpense}>
.....

<lightning-button
    class="slds-float_right"
    variant="brand"
    type="submit"
    label="Save">

Now, when you access fields it should have the appropriate values and you can attempt to add an extra field

handleSaveNewExpense(event){
        event.preventDefault();
        fields = event.detail.fields; //provided by onsubmit
        fields["CardKeeper__c"] = this.currentContact.Id;
        this.template.querySelector('lightning-record-edit-form').submit(fields);
    }
3
  • Thank you, everything is as you wrote! I have already started to dig in this direction... Only here a new field is not added in this way. As a result, only three fields from the form are sent to fields for recording. Jul 8 at 15:58
  • just tested it out, had no issues adding another field + value that wasn't on the form. Tested with lookup as well, but I was hard-coding the Id. Have you checked that this.currentContact.Id is not null? Or is CardKeeper__c the correct API name (ex. not Card_Keeper__c)? You can also use console.log(JSON.stringify(fields)) before and after adding the field to see that it looks right. Jul 8 at 17:04
  • Thank you for your help, I figured it out! I did everything as you wrote. The problem was related to the fact that currentContact is an array (I got it from another component) and I needed to specify this.currentContact[0].Id, but I was referring to the Id - this.currentContact.Id Jul 8 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.