Introduction

import { LightningElement, api } from 'lwc';
export default class TestClass extends LightningElement {
    @api recordId;
		@api objectApiName;
}
...
<div class="slds-var-m-around_medium">
    <lightning-record-form
        object-api-name={**objectApiName**}
        record-id={**recordId**}
        fields={fields}
    ></lightning-record-form>
</div>
...

<aside> 🔥 Important: both properties are set only when you place or invoke the component in an explicit record context. In all other cases, the recordId isn’t set, and your component shouldn’t depend on it.

</aside>

Access Record Context in Communities

Communities don’t automatically bind recordId to a component’s template.

To achieve that goal with your component, add recordId in an expression in the component’s *.js-meta.xml file. All the page types specified in the targets parameter have access to the record ID.

<targetConfigs>
    <targetConfig targets="lightningCommunity__Default">
        <property
            name="recordId"
            type="String"
            label="Record Id"
            description="Automatically bind the page's record id to the component variable"
            default="{!recordId}" />
    </targetConfig>
</targetConfigs>

When your component is invoked in a record context in a community, the expression {!recordId} is set to the 18-character ID of the record.