9

I am very new to Salesforce and I am stuck using LWC with communities. The problem is simple:
I generated my record list and when I click on the record I would like to access the selected record information on a detail page.
It works well when I hardcode the recordId but when using the @api recordId, the object I get is empty and recordId is undefined. I am trying to display it on a custom Object page.

When trying to do the same in Salesforce itself, it works as I can reference the object in the meta XML.

Here is my code: JS

import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import MEETING_NAME_FIELD from '@salesforce/schema/Meeting__c.Name';

export default class ComMeetingHeader extends LightningElement {
    @api recordId;

  @wire(getRecord, { recordId: '$recordId', fields: [MEETING_NAME_FIELD] })
  record;

  get meetingData() {
    return getFieldValue(this.record.data, MEETING_NAME_FIELD);
  }
}

HTML (object record seems to exist as it goes inside the if statement)

<template>
  <h1>{meetingData}</h1>
  <lightning-card title="Wire Function" icon-name="standard:contact">
      <template if:true={record}>
          <div class="slds-m-around_medium">
            <h1>Inside record</h1>
          </div>
      </template>
      <template if:true={error}>
          <h1>Inside error</h1>
          <div>{error}></div>
      </template>
  </lightning-card>
</template>

XML

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="comMeetingHeader">
  <apiVersion>45.0</apiVersion>
  <isExposed>true</isExposed>
  <targets>
    <target>lightning__RecordPage</target>
    <target>lightningCommunity__Page</target>
    <target>lightningCommunity__Default</target>
  </targets>
</LightningComponentBundle>

Thanks!

7
  • Can you add <target>lightning__RecordPage</target> to the XML and try Mar 7, 2019 at 18:48
  • I tried your meta, and was able to save it and deploy in communities. It might be, it does not allow you to add that property to a component which is already used in active community. Can you remove your component from community builder and then save it? Mar 7, 2019 at 20:00
  • @PranayJaiswal I did remove it saved and put it back but still no data displayed
    – MehdiKit
    Mar 7, 2019 at 20:18
  • @PranayJaiswal it is a custom object page where I put the component
    – MehdiKit
    Mar 7, 2019 at 20:20
  • @PranayJaiswal after removing it and uploading the config with only the lightning__RecordPage I can upload it (see updated post description) but I am still not able to retrieve the data.
    – MehdiKit
    Mar 8, 2019 at 8:55

3 Answers 3

21

From docs, you have to add <target>lightning__RecordPage</target> in the lwc component meta, for it to accept recordId from the detail page.

<targets>
    <target>lightning__RecordPage</target>
</targets>
Indicates that the component takes a record Id as an attribute.

EDIT: Apparently this works in App builder, but in communities, things are a bit funny.

You have to explicitly expose the recordIdusing <targetConfigs>

    <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
            <property name="recordId" type="String" ></property>
        </targetConfig>
    </targetConfigs>

Then in community builder provide the recordId as {!recordId}

9
  • I cannot use lightning_RecordPage in a community. When I do so I got the error:
    – MehdiKit
    Mar 7, 2019 at 19:48
  • 1
    You can't remove the lightningCommunity__Page from targets. You can't remove the lightningCommunity__Default from targets. This component is found in 2 Development Instance(s) and 1 Published Instance(s). Use the following component paths to locate the component: [Development] Full custom community > Record Detail (Widget) > header (Panel) > comMeetingHeader (Aura) [Development] Full custom community > Meeting Detail (Widget) > header (Panel) > comMeetingHeader (Aura) [Published] Full custom community > Meeting Detail (Widget) > header (Panel) > comMeetingHeader (Aura)
    – MehdiKit
    Mar 7, 2019 at 19:49
  • I did it already under XML
    – MehdiKit
    Mar 7, 2019 at 19:51
  • Here you go. I put the lightning__RecordPage together with the community tags otherwise I have the error I showed you.
    – MehdiKit
    Mar 7, 2019 at 19:56
  • I forgot to mention that the object record exists as it enters the if statement.
    – MehdiKit
    Mar 7, 2019 at 19:58
2

you might just be missing {!recordId} in the dialogue box when you add a component to community builder. For me that was the case as shown in the picture

0

Not sure but for me '$recordId' did not work in wired getrecord function. I was getting recordid data in renderedcallback() and in connectedcallack() i was getting it as undefined. To resolve this, I wrote one apex method to fetch data related to record and called that apex method from renderedcallback() Let me know if this helped to you.

1
  • It works.. just tried today
    – NITHESH K
    Jan 19, 2022 at 18:50

You must log in to answer this question.

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