6

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!

  • Can you add <target>lightning__RecordPage</target> to the XML and try – Pranay Jaiswal Mar 7 '19 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? – Pranay Jaiswal Mar 7 '19 at 20:00
  • @PranayJaiswal I did remove it saved and put it back but still no data displayed – MehdiKit Mar 7 '19 at 20:18
  • @PranayJaiswal it is a custom object page where I put the component – MehdiKit Mar 7 '19 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 '19 at 8:55
12

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}

| improve this answer | |
  • I cannot use lightning_RecordPage in a community. When I do so I got the error: – MehdiKit Mar 7 '19 at 19:48
  • 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 '19 at 19:49
  • I did it already under XML – MehdiKit Mar 7 '19 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 '19 at 19:56
  • I forgot to mention that the object record exists as it enters the if statement. – MehdiKit Mar 7 '19 at 19:58
0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.