[SalesForce] Get RecordId in LWC From Community Page

I'm trying to make a component in my community be able to pull in the record Id.

I followed the information in this question: LWC Community recordId undefined

And this documentation: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.use_config_for_community_builder

My config file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>ContractorSearch</masterLabel>
    <description>Searches for contractors for a given set of criteria</description>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordPage">
            <objects>
                <object>Requisition__c</object>
            </objects>
        </targetConfig>
        <targetConfig targets="lightningCommunity__Default">
            <property name="recordId" default="{!recordId}" type="String" 
                label="Record ID" description="Should not be changed from default value."/>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

Any ideas?

Best Answer

Apparently Salesforce is not clever enough to understand when you put in {!recordId} as a default value.

Removing the default value and setting the property in the community fixed the issue.

<property name="recordId" type="String" 
    label="Record ID" description="The value should be {!recordId}."/>

Update:

For people stumbling across this answer in the future, here is the full code of the component I made that is 100% functional.

Remember to set the variable in the component properties within your community:

enter image description here

JS:

import STATE_FIELD from '@salesforce/schema/Requisition__c.State__c';
//... for all fields

const requisitionFields = [
    STATE_FIELD, //... For all fields
];

@api recordId;
@track requisition;

@wire(getRecord, {recordId:'$recordId', fields:requisitionFields})
    getRequisition({error, data}) { 
        if(data) {
            this.requisition = data;
        }
    }
}

Update 2

A similar question was posted here. I have added another answer with copy/pasta code. The information is the same as what can be found in this answer and in the component I provided on github above; however, it is worded differently. If you are having issues with the answer above you can review the answer on the other question as well.

Related Topic