3

I'm trying to get the sObject Name on community record page. Getting the recordId works! But getting the sObject name does not.. Can I get it automatically in community ?

followed this doc which says its possible : https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.use_object_context

Thats my Lightning Web component markup:

XML :

`<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>My LWC</masterLabel>
    <description>Community Component</description>
     <targets>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
    <targetConfigs>
    <targetConfig targets="lightningCommunity__Default">
        <property name="recordId" type="String" label="Record ID" default="{!recordId}" description="The value should be {!recordId}."/>
        <property name="objectApiName" type="String" label="Object Name" description="Bind the page's {!objectApiName} to the component variable" default="{!objectApiName}" />
    </targetConfig>
    </targetConfigs>
</LightningComponentBundle>`

JS

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

  connectedCallback() {
    console.log('recordId ' + this.recordId); // this works
    console.log('objectApiName ' + this.objectApiName); // this does not WORK ??
  }
}
8
  • 1
    Did you follow the instructions for the metadata binding for objectApiName?
    – Phil W
    Commented Dec 16, 2019 at 23:34
  • What does this means? Not sure I understand.
    – sfdx bomb
    Commented Dec 17, 2019 at 7:00
  • Sorry, I now see that your "component markup XML" wasn't your template (which I assumed it was and ignored) but rather is the metadata XML file with the property binding for objectApiName. Given you did this, please can you add the URL for the page it isn't working in? I ask because the page you linked specifically says "This approach works only for components where the {!objectApiName} is in the route" which tells me the page URL must include the object type as a step for this to work.
    – Phil W
    Commented Dec 17, 2019 at 8:06
  • Which Page URL? Can you be a little more clear. My example followed the documentation link posted - the whole point here is to get the objectApiName dynamically.. if I need to mention the name specifically (hard code) it’s going against the idea here.
    – sfdx bomb
    Commented Dec 17, 2019 at 8:23
  • I literally mean the URL shown in the browser when you visit the page, in the community, where you see the problem with your component.
    – Phil W
    Commented Dec 17, 2019 at 9:51

2 Answers 2

3

From the discussion on this question it appears that there is a bug in the community builder that doesn't ensure the objectApiName value is made available, via the target property binding in the LWC's meta XML, unlike the recordId which seems to work.

The documentation does state, under Access Object Context in Communities, that a property must be used and includes the example:

<targetConfigs>
    <targetConfig targets="lightningCommunity__Default">
        <property
            name="objectApiName"
            type="String"
            label="Object Name"
            description="Automatically bind the page's object name to the component variable"
            default="{!objectApiName}" />
    </targetConfig>
</targetConfigs>

It is clear that this should work without explicitly stating the object's API name due to the default here (as per recordId).

The only caveat specified is:

NOTE This approach works only for components where the {!objectApiName} is in the route.

Either there's a bug or the specific usage here doesn't have the object API name as part of the URL.

5
  • This is not a bug. This mean tho work like this. Please see the below answer. Hope this will help. Thanks !!
    – Suyash
    Commented Apr 5, 2020 at 10:04
  • Hi. Please include link, in your answer, to documentation that explains this is the intention. Otherwise how can you know it isn't a bug?
    – Phil W
    Commented Apr 5, 2020 at 11:10
  • Hi Phil, I didn't find any specific documents which clearly states that but with my previous experience in Aura component this is how the behaviour was and same worked here also for me. This might help you. developer.salesforce.com/docs/component-library/documentation/… Thanks!!
    – Suyash
    Commented Apr 6, 2020 at 7:26
  • Sadly this is still an issue in 2022 :(
    – zaitsman
    Commented May 11, 2022 at 4:27
  • 2023 still an issue)
    – ytiq
    Commented Oct 6, 2023 at 11:30
0

Communities don’t automatically bind objectApiName to a component’s template like it does for recordId. With the below property:

We need to explicitly set the objectApiName in the community builder like below.

enter image description here

Hope this will help. Thanks

1
  • what you say goes against what the documentation states, except for the case where the object API name isn't part of the URL. I have updated my answer accordingly.
    – Phil W
    Commented Apr 6, 2020 at 7:43

You must log in to answer this question.

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