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}

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