Skip to main content

Salesforce - Lightning Components in Communities

    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

Popular posts from this blog

How do I learn to program?

The Question This question is asked often. You will see many answers, some even contradict each other. Here are a few I have seen. "It really depends on what your overall goals are." "Start with an easy language like..." "Start with a harder language like..." "Just start" All of these answers have valid reasons, but they do not help someone get started. I see this question come up over and over. I started to wonder how did I learn? How did other people learn? Finding Answers I set out on a journey to find an answers. I started asking programmers questions. The answers I found were fascinating. One question I asked was "How did you learn to program? Where you self taught or did you learn in school?" 90% of the responses said self taught. My first interesting enlightenment. This shouldn't of been such a big surprise. After all I was self taught. My second enlightenment came when I was trying to find a way to...

Eating an elephant, Breaking Code into tiny bites.

Most people have heard the joke “How do you eat an elephant? One bite at a time.” When programming we often end up biting off huge pieces and trying to shovel them down.  We start by building. We are almost always figuring out how to make it work. There is no book that says “If you need to create a sales report here is the code…” The reason is simple, every situation is different. One company might only care about the number of sales in a month. Others might want to track where the sales came from, what interactions led to the sale, the dollar amount, etc. (This later scenario is more likely) We end up with code that is cluttered, is not very optimized, and just plain ugly. What are we going to do to fix it? This is where breaking it into small pieces comes in. In my programming, it is rare for me to have a method that has more than 10 lines of code. Yes, you read that right no more than 10 lines of code PER method . I have had programmers tell me that is impossible. It’...

Why I Code

I love to learn. I am always seeking new knowledge. One of the great things I have learned is that coding is a journey. There is always more to learn. I have spent the last 20 years of my life on this amazing journey and will continue on it. When we start to learn how to code we learn some great problem solving skills. Coding is not just about typing on the keyboard. It is about taking a problem and breaking it into pieces. We take each of these pieces and turn them into solutions to the big problem. In the end we have made an amazing discovery within ourselves. We have learned how to solve the problems of our world.