5

Responding to mark as duplciate: See below. This is not the same question as the one I am saying did not have a working answer.

This question was asked here, but the answer didn't work for either me or another person, so I figured I'd ask it again in case anyone has a solution.

I'd like to get a recordId from the URL on a standard record detail page in a community. As best I can tell from documentation, what I have should be correct:

.js file:

import { LightningElement, track, api } from 'lwc';
....
 @api recordId;

.xml file:

<targets>
    <target>lightningCommunity__Default</target>
    <target>lightningCommunity__Page</target>
    <target>lightning__RecordPage</target>
</targets>
<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>

Whenever recordId is referenced it throws a fatal error that the variable is undefined. Any guesses as to how to fix this?

7
  • 4
    Possible duplicate of Get RecordId in LWC From Community Page As stated in this question, the issue is that you cannot set a default value in your config. Remove the default value and in your community when adding your component to the detail page set the value in the component properties to {!recordId}
    – gNerb
    Commented Oct 30, 2019 at 21:50
  • 1
    That doesn't fix it for me. I've tried without the default, with the default and with the default without the bang. No luck so far.
    – Apexing
    Commented Oct 30, 2019 at 22:37
  • I updated my answer to have more information including the full code used in the component I was working on as well as screenshots. If you haven't gone through the additional information I suggest you do. If the information in that answer doesn't work for you you will want to update your question with specific troubleshooting steps and a more detailed code sample.
    – gNerb
    Commented Oct 30, 2019 at 22:40
  • Im facing the same issue as @Rozgonyi on there. What do you get for this.recordId?
    – Apexing
    Commented Oct 30, 2019 at 22:50
  • That's a difficult question to answer. LWC components operate asyncronously so if you access it at the wrong time it could be undefined but if you access it at the correct time it could show you the correct value. If you review my component it shows one such way of correctly referencing that value. If you need more specific help you need to provide more specific code. Show us what you are actually trying to do and we might be able to help.
    – gNerb
    Commented Oct 31, 2019 at 14:28

1 Answer 1

8

This is the minimum code example I can provide to show how to work with a record ID. I have deployed this code and verified it works from within a community. This code is copy-pasta. If this code does not work for you chances are you are not configuring your component correctly in the community:

JS:

import { LightningElement, api } from 'lwc';

export default class RecordComponent extends LightningElement {
    @api recordId

    handleConsoleLogClick(event) {
        console.log(this.recordId);
    }
}

js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="recordComponent">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
            <property name="recordId" type="String" label="Record ID"
                description="Should be set to {!recordId}"/>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

html

<template>
    <lightning-button 
        variant="brand" 
        label="Console.log" 
        title="Console.log" 
        onclick={handleConsoleLogClick} 
    ></lightning-button>
</template>

Results:

Render

Console Log

Requirements

You must configure your component within the community to use the {!recordId} value provided by the page:

Select the component:

Select the Component

Configure the component:

Configure the component

This value can be provided in one of two ways that I know of.

  1. The page can contain a url paramter called recordId
  2. The component can be embeded onto the same page as a record detail component. This component sets the recordId value.

Not the answer you're looking for? Browse other questions tagged .