Recently I built a Lightning component, tested it as in
internal user, and it was beautiful. Everything worked as expected. The work
was turned over to the project manager to demo to the client. During the demo
nothing worked, just errors. After the demo I received the call. “What did you
build? Why didn’t it work?”
I couldn’t understand what had happened. I asked him to show
me how he had demonstrated it. He went straight to the community builder, plopped
my component on the page, and ERROR! Unbeknownst to me the recordId does not
pass to lightning components in the community the way it does as an internal
user.
Now you get to learn what I learned.
The problem:
Force:hasRecordId does not work in community builder.
The Record ID is not passed by default in the community builder.
However, there is a workaround to use it.
The Solution:
In your component add force:hasRecordId. This is not needed
for community builder; however, it ensures it still works in the internal org
as a component on the page layout.
<aura:component implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes,force:hasRecordId" >
Add the recordId attribute (if you don’t already have it)
Note: The type should be string, not Id
<aura:attribute name="recordId"
type="String"/>
In the Design for your component add
<design:attribute name="recordId" label="Record
Id" default="{!recordId}" description="Salesforce Id of the
record" />
DONE!
Now you can use v.recordId in your component.
As an example, I have included a piece of JavaScript you can
add to your Lightning controller. This simply will output the record ID to the
console.
({
getRecordID :
function(component, event, helper) {
console.log(component.get('v.recordId'));
}
})
Comments
Post a Comment