0

Suppose I have two custom objects: Property and Broker. Property has a lookup field to Broker. Suppose now I need to create a custom aura component inside a property record page that shows only the empty fields of the Broker lookup record in a lightning:recordForm. Using LDS I could declare two attributes:

<aura:attribute name="recordId" type="Id" />
<aura:attribute name="simpleRecord" type="Object" />

And a <force:recordData> tag to get the fields from that property record:

<force:recordData aura:id="recordLoader"
        recordId="{!v.recordId}"
        targetFields="{!v.simpleRecord}"
        layoutType="FULL"
        recordUpdated="{!c.recordUpdated}" />

I could show the Broker fields by using lightning:recordForm:

<lightning:card iconName="standard:user" title="{! 'Broker for ' + v.simpleRecord.Name}">
        <div class="slds-p-left_large slds-p-right_medium">
            <lightning:recordForm aura:id="brokerForm" mode="VIEW" recordId="{!v.simpleRecord.Broker__c}"
                objectApiName="Broker__c"
                layoutType="Full"
                columns="2" />
        </div>
    </lightning:card>

But I need to only show the fields which are empty. I can do something like this in the Javascript Controller, but I need to get the fields from the Broker record, not the Property record:

recordUpdated: function (component, event, helper) {

        var eventParams = event.getParams();

        if (eventParams.changeType === "LOADED") {
            // record is loaded (render other component which needs record data value)

            const json = JSON.parse(JSON.stringify(component.get('v.simpleRecord')));
            let emptyFields = []

            for (const [fieldName, fieldInfo] of Object.entries(json.fields)) {
                if (fieldInfo.value === null) {
                    emptyFields.push(fieldName);
                }
            }

            console.log('emptyFields: ', emptyFields);

        }
}

Also, in the json I can see some of the Broker fields inside the Broker__r value, but only Id and Name.


In summary, how do I get the fields from a related object, but only the empty fields, using only Lightning Data Service, not apex?

Thanks for your attention.

Your Answer

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

Browse other questions tagged or ask your own question.