Get Record Id in Lightning Web Component

Sharing is Caring

Once you know how to do it, getting the current Record Id in a lightning component or lightning aura component is pretty easy. We simply need to implement the force:hasRecordId interface in the lightning component.

Getting the current record id is really useful if we are going to be using it on a lightning record page, or using it to do an action override in lightning experience, or using it in the salesforce app.

The recordId attribute is set only when the component is placed or invoked from a record page or an object’s home page. If the component is going to be placed programmatically inside other components the recordId won’t be set.

In a cmp file it would look this:

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

Keep in mind that this interface is a marker interface. A marker interface is a signal to the component’s container to add the interface’s behaviour to the component.

We don’t need to implement any specific methods or attributes in the component, you simply add the interface name to the component’s implements attribute.

Sharing is Caring

Brian is a programmer and technology leader living in Niagara Falls with more than ten years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.