This seems to be pretty simple but I kinda lost right now. I want to create a LWC based on two custom objects which has a master-detail relationship.
I have a 'master' object Product__c
- Name
- Id
- etc
and the 'detail' object Product_Version__c
- Name
- Id
- Product__c (Master-Detail (Product))
- Pre_Version__c (Lookup(Product Version))
- etc
I need to get field 'Id' and 'Pre_Version__c' from Product_Version__c based on the Product__c.Id
I know the SOQL command to get them:
SELECT (SELECT Id, Name, Pre_Version__c From Product_Versions__r),Name FROM Product__c WHERE Id = 'abcde'
I also know how to use wire to get fields from an object, but not from the child/detail object.
How do I get fields from the child/detail object (Product_Version__c) based only on Product__c.Id?
lwc.js
import { LightningElement,api,wire} from 'lwc';
import { getRecord, getRecordUi } from 'lightning/uiRecordApi';
import productVersion from '@salesforce/schema/Product_Version__c';
const fields = [
'Product__c.Name',
'Product__c.Id'
];
const productVersionFields = [
'Product_Version__c.Name',
'Product_Version__c.Id',
'Product_Version__c.Pre_Version__c',
]
export default class ProductVersion extends LightningElement {
@api recordId;
recordId= 'abcde';
@wire(getRecord, { recordId: '$recordId', fields })
product;
get name() {
return this.product.data ? this.product.data.fields.Name.value : "";
}
get id() {
return this.product.data ? this.product.data.fields.Id.value : "";
}
@wire(getRecordUi, { recordIds: '$recordId', childRelationships: "Product.Product_Versions" })
productVersion;
//Product Version object
get preVersion() {
return this.productVersion.data ? this.productVersion.data.fields.Pre_Version__c .value : "";
}
get childId() {
return this.productVersion.data ? this.productVersion.data.fields.Id.value : "";
}
}