0

I have tried to create LWC's for a record creation/ Update like below.

Parent LWC component,

<template>
  <lightning-button variant="brand" label="Click Here" title="Primary action" onclick={handleClick} class="slds-m-left_x-small"></lightning-button>
  <c-object-type-selection></c-object-type-selection>
</template>

Parent LWC component JS.

import { LightningElement, api } from 'lwc';

export default class ParentSampleButton extends LightningElement {

  handleClick() {
    const typeSelection = this.template.querySelector('c-object-type-selection');
    typeSelection.show();
  }
}

object-type-selection component

<template>
  <template if:true={showModal}>
    <article class="slds-card">
      <div style="height: 1000px;">
        <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
          <div class="slds-modal__container">
            <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1" style="height: 640px;">
              <lightning-combobox
                name="Type"
                label="Type"
                value={type}
                placeholder={type}
                options={typeSelect}
                required="true"
                onchange={typeChange}>
              </lightning-combobox>
              <br/>
              <div>
                <template if:true={selected}>
                  <c-create-plan-object obj-type={value} parent-id={parentId} type={type} field-set-fields={retrievedFields} record-id={recordId} fields={fieldsToPass}></c-create-plan-object>
                </template>
              </div>
            </div>
            <footer class="slds-modal__footer">
              <button class="slds-button slds-button_brand" onclick={handleSubmit} disabled={hideButton}>Save</button>
            </footer>
          </div>
        </section>
      </div>
    </article>
  </template>
</template>

JS file

import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from "lightning/platformShowToastEvent";

export default class ObjectTypeSelection extends LightningElement {

  retrievedFields; // fill from an apex call
  fieldsToPass;
  @api show() {
    this.showModal = true;
  }

  typeChange(event) {
   
  }

  handleSubmit(event) {
    event.preventDefault();
    this.fieldsToPass = event.detail.fields;
    console.log('AAAA ' + this.fieldsToPass);
    var component = this.template.querySelector('c-ppm-create-plan-object');
    component.handleSubmitChild();
  }
}

create-plan-object component

<template>
  <lightning-record-edit-form
    object-api-name={objType}
    <template for:each={fieldSetFields} for:item="fieldSetField">
      <lightning-input-field field-name={fieldSetField.apiname} key={fieldSetField.apiname} data-field={fieldSetField.apiname}>
      </lightning-input-field>
    </template>
  </lightning-record-edit-form>
</template>

JS file

import { LightningElement, api, wire } from 'lwc';

export default class CreatePlanObject extends LightningElement {

  @api fieldSetFields;
  @api fields;

  @api
  handleSubmitChild() {

    if(this.parentId == 'global') {
      if(this.objType == 'Portfolio__c') {
        fields.Portfolio__c = this.recordId;
      }
    }

    this.template.querySelector('lightning-record-edit-form').submit(fields);
  }
}

How we can get the lightning-record-edit-form to the parent object-type-selection component to submit via the save button which also exists on the parent object-type-selection component?

5
  • I've done the same, but the fields were not coming via event.detail.fields. may be I have missed something. Commented Feb 17, 2022 at 5:41
  • well, you are not passing anything to your method, so, event.detail.fields is going to be undefined every single time in your child
    – glls
    Commented Feb 17, 2022 at 12:27
  • Edited the question. In my scenario the buttons are in the parent component and it is outside the lightning-record-edit-form tag which is in the child component. Whenever we save the record edit form from that we can override the default submit behavior where we can get the fields from the event. But in this case I cannot able to get the event.detail.fields from the button click. Commented Feb 17, 2022 at 13:44
  • 80% of the code you have posted is irrelevant to the question. if you minimize it to only what matters, it will be easier to read and provide feedback. I highly doubt that anyone would want to go through all those lines of code.
    – glls
    Commented Feb 17, 2022 at 14:02
  • edited the question, yes I've posted the whole code to explain the scenario. I am just trying to understand the lightning-record-edit-form behavior and it's not working when the button on the parent and the lightning-edit-form on the child. May be the communication between those two components was not set properly, But trying to understand what I have missed here. Commented Feb 17, 2022 at 15:41

0

You must log in to answer this question.