10

Anyone found a way to select parent fields with force:recordData? It would be great to have something like this:

<force:recordData recordId="{!v.recordId}" targetRecord="{!v.child}"
                  layoutType="FULL" fields="parent__r.Name" recordUpdated="{!c.init}" />

init: function(cmp, evt, helper) {
    var child= cmp.get("v.child");

    if(child) {
        console.log(child.fields.parent__r.Name);
    }
}

results is undefined

  • 1
    What does this prints?- JSON.stringify(child.fields) – Raul Oct 18 '17 at 11:33
  • threw an error, but log(child.fields) worked. So it seems like this did the trick: child.fields.parent__r.value.fields.Name.value doesn't look good though :/ – Basti Oct 18 '17 at 12:01
  • 1
    Looks like the structure ob object generated by Salesforce here is bit odd. – Raul Oct 18 '17 at 12:06
  • couldn't find any other way, thanks for your help! – Basti Oct 18 '17 at 12:08
  • No worries, glad to be of any help. Its looks ugly though :D – Raul Oct 18 '17 at 12:18
14

So with some help I figured out that it works, it is just a bit dirty to get the values, this is the path through the object structure:

child.fields.parent__r.value.fields.Name.value

So the targetFields attribute on recordData just came to my mind, and I replaced targetRecord. Now it works as expected:

<force:recordData recordId="{!v.recordId}" targetFields="{!v.child}"
                  fields="parent__r.foo__c" recordUpdated="{!c.init}" />

var child = cmp.get("v.child");
console.log(child.parent__c); 
console.log(child.parent__r.Id); 
console.log(child.parent__r.Name); 
console.log(child.parent__r.foo__c); 

Notes:

  • parent__r.Name and parent__r.Id will ALWAYS be part of the data set
  • Don't forget the namespace: MY_NAMESPACE__parent__r.MY_NAMESPACE__foo__c

The max depth is 5

parent__r.parent2__r.parent3__r.parent4__r.parent5__r.bar__c

Exceeding the limit will silently fail and leave your targetFields="{!v.child}" null

  • Same as when you mistype field names in the fields="" attribute (or forgot the NS) it will tell you the miss-leading error message:

    [Cannot read property 'parent__r' of null]

Edit

Found it officially documented, hidden in the Lightning Components Developer Guide:

Lightning Data Service supports spanned fields with a maximum depth of five levels. Support for working with collections of records or for querying for a record by anything other than the record ID isn’t available. If you must support higher-level operations or multiple operations in one transaction, use standard @AuraEnabled Apex methods.

| improve this answer | |
  • When I add a parent field to the fields attribute of force:recordData it is not the callback for recordUpdated. But there is no error in target error. Did you encounter this behaviour? – ShreyasD Mar 19 '19 at 17:21
  • Do you mean the update method is not called when the parent gets updated, or is it not queried when the record itself gets updated? I don't remember any issues, but I'm not sure if I had this scenario. – Basti Mar 20 '19 at 8:57
  • My mistake I was using the wrong API name for a related field. But it did not give an error. But just failed to callback on the recordUpdated event :|. With the correct API name it works fine. – ShreyasD Mar 21 '19 at 17:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.