Skip to main content
Notice removed Authoritative reference needed by Anjali
Bounty Ended with RubenDG's answer chosen by Anjali
deleted 39 characters in body
Source Link
Anjali
  • 247
  • 4
  • 22

enter image description here

LWC:

enter image description here

LWC:

LWC:

Notice added Authoritative reference needed by Anjali
Bounty Started worth 50 reputation by Anjali
added 6509 characters in body
Source Link
Anjali
  • 247
  • 4
  • 22

JS:

import { LightningElement, track, api } from 'lwc'; import CreationChild from '@salesforce/apex/CreationController.CreationChild'; import getForm from '@salesforce/apex/CreationController.getForm'; import getRecordTypeId from '@salesforce/apex/CreationController.getRecordTypeId'; import getRecordTypeDevName from '@salesforce/apex/CreationController.getRecordTypeDevName'; import { RefreshEvent } from 'lightning/refresh'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import { NavigationMixin } from 'lightning/navigation'; export default class CreationLwc extends NavigationMixin(LightningElement) { DisplayText = false; @api size; @api objectName; @api recordId; @api fieldSet; @track fields; @track systemfields; @track isModalOpen = false; @api PortfolioRecordTypeName; @api PortfolioRecordTypeId; @api PortfolioRecordTypeDevName = 'Test'; @api accrecordId; @api showOFields = false; @api showTFields = false; @api portAccountId; @api selectedPortfolioId; @api showPortfolio = false; isLoading = false; @api isSystemFields = false; account; openModal() { this.isModalOpen = true; } closeModal() { this.isModalOpen = false; this.dispatchEvent(new RefreshEvent()); } submitDetails() { this.isModalOpen = false; } handleSuccess(event) { console.log('onsubmit event recordEditForm' + event.detail.id); this.createRelationship( event.detail.id, this.accrecordId, 'Owner'); this.navigateToRecordPage(); } handleOTChange(event) { console.log('You selected an account: ' + event.detail.value); if (event.detail.value === 'Owner') { this.getFieldsForOwner(); this.getFieldsForChild(); this.showOFields = true; this.showTFields = false; this.showPortfolio = false; //this.connectedCallback(); } } else if (event.detail.value === '') { this.showOFields = false; this.showTFields = false; } } getFieldsForOwner() { //this.template.querySelector('c-modal').show(); getForm({ recordId: this.recordId, objectName: 'parent__c', fieldSetName: this.PortfolioRecordTypeDevName }) .then(result => { console.log('Data:' + JSON.stringify(result)); if (result) { this.fields = result.Fields; this.error = undefined; } }) .catch(error => { console.log(error); this.error = error; }); } validateFields() { return [...this.template.querySelectorAll('lightning-input-field')].reduce((validSoFar, field) => { return validSoFar && field.reportValidity(); }, true); } handleSubmit(e){ const inputFields = e.detail.fields; this.template.querySelector('lightning-record-edit-form').submit(inputFields); } handleError(e) { //this.template.querySelector('[data-id="message"]').setError(e.detail.detail); e.preventDefault(); } closeComponent() { const close = new CustomEvent('close'); this.dispatchEvent(close); } navigateToRecordPage() { this[NavigationMixin.Navigate]({ type: "standard__recordPage", attributes: { actionName: "view", recordId: this.accrecordId } }); } handleEmpty(event) { this.account = event.detail; } getFieldsForChild() { getForm({ recordId: this.recordId, objectName: 'child__c', fieldSetName: this.PortfolioRecordTypeDevName }) .then(result => { console.log('Data:system fields' + JSON.stringify(result)); if (result) { this.systemfields = result.Fields; this.isSystemFields = true; this.error = undefined; } }) .catch(error => { console.log(error); this.error = error; }); } } 

JS:

import { LightningElement, track, api } from 'lwc'; import CreationChild from '@salesforce/apex/CreationController.CreationChild'; import getForm from '@salesforce/apex/CreationController.getForm'; import getRecordTypeId from '@salesforce/apex/CreationController.getRecordTypeId'; import getRecordTypeDevName from '@salesforce/apex/CreationController.getRecordTypeDevName'; import { RefreshEvent } from 'lightning/refresh'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import { NavigationMixin } from 'lightning/navigation'; export default class CreationLwc extends NavigationMixin(LightningElement) { DisplayText = false; @api size; @api objectName; @api recordId; @api fieldSet; @track fields; @track systemfields; @track isModalOpen = false; @api PortfolioRecordTypeName; @api PortfolioRecordTypeId; @api PortfolioRecordTypeDevName = 'Test'; @api accrecordId; @api showOFields = false; @api showTFields = false; @api portAccountId; @api selectedPortfolioId; @api showPortfolio = false; isLoading = false; @api isSystemFields = false; account; openModal() { this.isModalOpen = true; } closeModal() { this.isModalOpen = false; this.dispatchEvent(new RefreshEvent()); } submitDetails() { this.isModalOpen = false; } handleSuccess(event) { console.log('onsubmit event recordEditForm' + event.detail.id); this.createRelationship( event.detail.id, this.accrecordId, 'Owner'); this.navigateToRecordPage(); } handleOTChange(event) { console.log('You selected an account: ' + event.detail.value); if (event.detail.value === 'Owner') { this.getFieldsForOwner(); this.getFieldsForChild(); this.showOFields = true; this.showTFields = false; this.showPortfolio = false; //this.connectedCallback(); } } else if (event.detail.value === '') { this.showOFields = false; this.showTFields = false; } } getFieldsForOwner() { //this.template.querySelector('c-modal').show(); getForm({ recordId: this.recordId, objectName: 'parent__c', fieldSetName: this.PortfolioRecordTypeDevName }) .then(result => { console.log('Data:' + JSON.stringify(result)); if (result) { this.fields = result.Fields; this.error = undefined; } }) .catch(error => { console.log(error); this.error = error; }); } validateFields() { return [...this.template.querySelectorAll('lightning-input-field')].reduce((validSoFar, field) => { return validSoFar && field.reportValidity(); }, true); } handleSubmit(e){ const inputFields = e.detail.fields; this.template.querySelector('lightning-record-edit-form').submit(inputFields); } handleError(e) { //this.template.querySelector('[data-id="message"]').setError(e.detail.detail); e.preventDefault(); } closeComponent() { const close = new CustomEvent('close'); this.dispatchEvent(close); } navigateToRecordPage() { this[NavigationMixin.Navigate]({ type: "standard__recordPage", attributes: { actionName: "view", recordId: this.accrecordId } }); } handleEmpty(event) { this.account = event.detail; } getFieldsForChild() { getForm({ recordId: this.recordId, objectName: 'child__c', fieldSetName: this.PortfolioRecordTypeDevName }) .then(result => { console.log('Data:system fields' + JSON.stringify(result)); if (result) { this.systemfields = result.Fields; this.isSystemFields = true; this.error = undefined; } }) .catch(error => { console.log(error); this.error = error; }); } } 
Source Link
Anjali
  • 247
  • 4
  • 22

How to create Parent and Child records using Lightning Edit Form in LWC?

I have a requirement where user should be able to create Parent and child record with single button click on one page. For this I created 2 lightning-record-edit-form on the LWC, but unable to find a way to pass the Parent Id to Child record form on submit. Can any one suggest some ideas on how to acheive it? In the below picture first 2 fields come from Parent and second 2 from child. On save both records should be saved, Child should be related to the Parent. I am using 2 forms because, child fields might vary based on the record type of parent.

enter image description here

LWC:

<template> <div class="modal-body"> <br /> <lightning-card> <lightning-record-edit-form record-id={recordId} object-api-name="ParentObject__c" record-type-id={parentId} onsuccess={handleSuccess} onsubmit={handleSubmit} onerror={handleError} > <lightning-messages> </lightning-messages> <template> <div class="slds-grid slds-wrap"> <!-- <template if:true={fields}>--> <template for:each={parentfields} for:item="f"> <div key={f.Id} class="slds-col slds-size_1-of-2"> <lightning-input-field field-name={f.APIName}> </lightning-input-field> </div> </template> <!--</template>--> </div> </template> </lightning-record-edit-form></lightning-card > <template> <template> <lightning-card title="Account/Site-Specific Fields"> <lightning-record-edit-form record-id={recordId} object-api-name="childObject__c" onsuccess={handleSuccess} onsubmit={handleSubmit} onerror={handleError} > <div class="slds-grid slds-wrap"> <!-- <template if:true={fields}>--> <template for:each={childfields} for:item="f"> <div key={f.Id} class="slds-col slds-size_1-of-2"> <lightning-input-field field-name={f.APIName}> </lightning-input-field> </div> </template> <!--</template>--> </div> </lightning-record-edit-form></lightning-card ></template > </template> <br /> </div> <div slot="footer"> <template> <lightning-button class="slds-m-top_small slds-p-right_medium" variant="brand" onclick={handleSubmit} name="save" label="Save"> </lightning-button ></template> </div> </template>