1

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
    Dec 16 '19 at 23:34
  • What does this means? Not sure I understand.
    – sfdx bomb
    Dec 17 '19 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
    Dec 17 '19 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
    Dec 17 '19 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
    Dec 17 '19 at 9:51
2

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.

3
  • This is not a bug. This mean tho work like this. Please see the below answer. Hope this will help. Thanks !!
    – Suyash
    Apr 5 '20 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
    Apr 5 '20 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
    Apr 6 '20 at 7:26
-1

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
    Apr 6 '20 at 7:43

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.