I am looking for verification/documentation on force:hasRecordId. It appears from my experience that recordId is only provided to the "container" component, and it does not feed down to any child components without explicitly passing the value.

Is that right?

BTW, I've read the docs here. They don't address my question.

  • Can you clarify with a snippet what you think it needs vs what you hope it does? – Sebastian Kessel Dec 4 at 18:38
  • @SebastianKessel, I have a component that is the 3rd great-grandchild (3gg.cmp) of my container component. I need 3gg.cmp to be aware of the recordId of the page it's on. Using force:hasRecordId doesn't work in this context. Instead, I have to put force:hasRecordId in the outermost component, and cascade the recordId value to each child component all the way down to 3gg.cmp. (I'm going to be explaining this to a group soon, and I wanted complete clarity so I'm not spewing BS.) – PatMcClellan__c Dec 5 at 19:11
up vote 6 down vote accepted

I think the documentation covers it, but it's not very explicit and relies on us to read into a couple of key words that aren't obviously key words.

A marker interface is a signal to the component’s container to add the interface’s behavior to the component

Emphasis mine. Later,

Important The recordId attribute is set only when you place or invoke the component in an explicit record context. For example, when you place the component directly on a record page layout, or invoke it as an object-specific action from a record page or object home.

Emphasis again mine.

So if we have Components A and B, here:

componentA.cmp

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <c:componentB />
</aura:component>

componentB.cmp

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    {! v.recordId }
</aura:component>

You get no output if you place this on a record page. (I did validate this directly).

But if componentA, which is componentB's container, supplies the record Id:

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <c:componentB recordId="{! v.recordId }" />
</aura:component>

You will indeed observe the output.

Only componentA is in an "explicit record context", which the documentation implicitly defines for us as a record page container or use as an object-specific action.

componentB is not in an explicit record context (it could be anywhere) and requires its container, componentA, to provide the recordId.

  • Another wonderful answer, David. I wish I had grokked what the OP needed with as little input as you did – Sebastian Kessel Dec 4 at 23:29
  • @David Reed, thanks for the clarity. So there's really no point in adding force:hasRecordId to child components, as you have to explicitly pass the recordId from the parent anyway. And in that context, recordId isn't a magic word, it's just an attribute name. Correct? – PatMcClellan__c Dec 5 at 19:07
  • @PatMcClellan__c I agree in the sense that it is not magic. I could see adding the interface to a reusable component that might be used both within some custom container (which would explicitly supply the record Id) or be placed directly on a record page. At worst, it does no harm; I think it's case-specific whether you'd want to use it. – David Reed Dec 5 at 19:12
  • @DavidReed, good point on reusability. Thanks again for your clarity on this. – PatMcClellan__c Dec 5 at 19:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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