You need to sign in to do that
Don't have an account?
Ap30
how to pass value from parent to child component in lwc
Hi All,
I'm new to lightning web components. In my below code, two objects Vendor__c and Payment__c ia having master detail relationship.Vendor__c is the parent object. On clicking submit button in VendorComponent, master detail field in payCompopnent should be populated with the value of the record just created in component 1. Please help to achieve this. Also email to be sent sent to vendor email id once payement is done in component 2.
VendorComponent
==============
<template>
<lightning-record-edit-form object-api-name="Vendor__c">
<lightning-input-field field-name="Vendor_First_Name__c"></lightning-input-field>
<lightning-input-field field-name="Vendor_Last_Name__c"></lightning-input-field>
<lightning-input-field field-name="Vendor_Phone__c" type="tel"></lightning-input-field>
<lightning-input-field field-name="Vendor_Email__c"></lightning-input-field>
<lightning-combobox
name="st"
label="State"
value={value1}
placeholder="Select State"
options={options1}
onchange={handleChange1} ></lightning-combobox>
<lightning-combobox
name="Com"
label="Company"
value={value2}
placeholder="Select Company"
options={options2}
onchange={handleChange2} ></lightning-combobox>
<lightning-button class="slds-m-top_small" variant="brand" type="submit" label="SAVE" onclick = {navigateToLightningComponent}></lightning-button>
</lightning-record-edit-form>
</template>
==============================================
import { LightningElement,wire,api} from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class VendorComponent extends LightningElement {
get options1() {
return [
{ label: 'Delhi', value: 'del' },
{ label: 'Maharashtra', value: 'mah' },
{ label: 'Kerala', value: 'ker' },
];
}
get options2() {
return [
{ label: 'Company1', value: '1c' },
{ label: 'Company2', value: '2c' },
{ label: 'Company3', value: '3c' },
];
}
navigateToLightningComponent() {
this[NavigationMixin.Navigate]({
"type": "custom__component",
"attributes": {
"componentName": "c__payComponent"
}
});
}
}
==============================================
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
========================
PayComponent
=========
<template>
<lightning-record-edit-form object-api-name="Payment__c">
<lightning-output-field field-name="Vendor_Details__c" value={vendorDet}></lightning-output-field>
<lightning-input-field field-name="Card_No__c" type="number"></lightning-input-field>
<lightning-input-field field-name="Exp_Month__c" type="number"></lightning-input-field>
<lightning-input-field field-name="Exp_Year__c" type="number"></lightning-input-field>
<lightning-input-field field-name="CVV__c" type="password"></lightning-input-field>
<lightning-button class="slds-m-top_small" variant="brand" type="submit" label="SUBMIT"></lightning-button>
</lightning-record-edit-form>
</template>
=============================================
import { LightningElement, wire,api,track } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
import { NavigationMixin } from 'lightning/navigation';
import { getRecord } from 'lightning/uiRecordApi';
import { getFieldValue } from 'lightning/uiRecordApi';
import VENDOR_OBJECT from '@salesforce/schema/Vendor__c';
import NAME_FIELD from '@salesforce/schema/Vendor__c.Name';
import PAYMENT_OBJECT from '@salesforce/schema/Payment__c';
const fields = [NAME_FIELD];
export default class PayComponent extends NavigationMixin(LightningElement) {
@api getIdFromParent;
@api vendorDet;
@wire(CurrentPageReference) pageRef;
@wire(getRecord, { recordId: '$getIdFromParent', fields: [NAME_FIELD] })
PAYMENT_OBJECT;
get vendorDet() {
return getFieldValue(this.PAYMENT_OBJECT.data, NAME_FIELD);
}
}
======================================================
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="paymentInfoComponent">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
I'm new to lightning web components. In my below code, two objects Vendor__c and Payment__c ia having master detail relationship.Vendor__c is the parent object. On clicking submit button in VendorComponent, master detail field in payCompopnent should be populated with the value of the record just created in component 1. Please help to achieve this. Also email to be sent sent to vendor email id once payement is done in component 2.
VendorComponent
==============
<template>
<lightning-record-edit-form object-api-name="Vendor__c">
<lightning-input-field field-name="Vendor_First_Name__c"></lightning-input-field>
<lightning-input-field field-name="Vendor_Last_Name__c"></lightning-input-field>
<lightning-input-field field-name="Vendor_Phone__c" type="tel"></lightning-input-field>
<lightning-input-field field-name="Vendor_Email__c"></lightning-input-field>
<lightning-combobox
name="st"
label="State"
value={value1}
placeholder="Select State"
options={options1}
onchange={handleChange1} ></lightning-combobox>
<lightning-combobox
name="Com"
label="Company"
value={value2}
placeholder="Select Company"
options={options2}
onchange={handleChange2} ></lightning-combobox>
<lightning-button class="slds-m-top_small" variant="brand" type="submit" label="SAVE" onclick = {navigateToLightningComponent}></lightning-button>
</lightning-record-edit-form>
</template>
==============================================
import { LightningElement,wire,api} from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class VendorComponent extends LightningElement {
get options1() {
return [
{ label: 'Delhi', value: 'del' },
{ label: 'Maharashtra', value: 'mah' },
{ label: 'Kerala', value: 'ker' },
];
}
get options2() {
return [
{ label: 'Company1', value: '1c' },
{ label: 'Company2', value: '2c' },
{ label: 'Company3', value: '3c' },
];
}
navigateToLightningComponent() {
this[NavigationMixin.Navigate]({
"type": "custom__component",
"attributes": {
"componentName": "c__payComponent"
}
});
}
}
==============================================
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
========================
PayComponent
=========
<template>
<lightning-record-edit-form object-api-name="Payment__c">
<lightning-output-field field-name="Vendor_Details__c" value={vendorDet}></lightning-output-field>
<lightning-input-field field-name="Card_No__c" type="number"></lightning-input-field>
<lightning-input-field field-name="Exp_Month__c" type="number"></lightning-input-field>
<lightning-input-field field-name="Exp_Year__c" type="number"></lightning-input-field>
<lightning-input-field field-name="CVV__c" type="password"></lightning-input-field>
<lightning-button class="slds-m-top_small" variant="brand" type="submit" label="SUBMIT"></lightning-button>
</lightning-record-edit-form>
</template>
=============================================
import { LightningElement, wire,api,track } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
import { NavigationMixin } from 'lightning/navigation';
import { getRecord } from 'lightning/uiRecordApi';
import { getFieldValue } from 'lightning/uiRecordApi';
import VENDOR_OBJECT from '@salesforce/schema/Vendor__c';
import NAME_FIELD from '@salesforce/schema/Vendor__c.Name';
import PAYMENT_OBJECT from '@salesforce/schema/Payment__c';
const fields = [NAME_FIELD];
export default class PayComponent extends NavigationMixin(LightningElement) {
@api getIdFromParent;
@api vendorDet;
@wire(CurrentPageReference) pageRef;
@wire(getRecord, { recordId: '$getIdFromParent', fields: [NAME_FIELD] })
PAYMENT_OBJECT;
get vendorDet() {
return getFieldValue(this.PAYMENT_OBJECT.data, NAME_FIELD);
}
}
======================================================
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="paymentInfoComponent">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
>>http://amitsalesforce.blogspot.com/2019/07/events-in-lightning-web-components-lwc.html
The above link has an implementation of sending parameters from parent to child and child to parent you can check the implementations and modify it to fit your needs.
Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.
Thanks.