How to get the recordId in lightning web component ?

If a component with a recordId property is used on a Lightning record page, the page sets the property to the ID of the current record.

import { LightningElement, api } from 'lwc';
export default class TestClass extends LightningElement {
    @api recordId;
}

If the componet is not in lightning record page or mobile app, the recordId will not be populated.

You can find full working example here.

To get the recordId in communities, we need to specify a parameter in targets in meta file of the component.

<targetConfigs>
    <targetConfig targets="lightningCommunity__Default">
        <property
            name="recordId"
            type="String"
            label="Record Id"
            description="Automatically bind the page's record id to the component variable"
            default="{!recordId}" />
    </targetConfig>
</targetConfigs>

When your component is invoked in a record context in a community, the expression "{!recordId}" is set to the the 18-character ID of the record.

Resources:
Salesforce Official Documentation

Please follow and like us:

1 Trackback / Pingback

  1. Wire Service in Lightning web component - auraEnabled

Comments are closed.