function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eric Latreille 8Eric Latreille 8 

LWC - @api recordId not working

I am not able to save my record. no error message is showing.
Here is my class.

public with sharing class ScheduleClass {
    @AuraEnabled
    public static Product_Schedule__c submitSchedule(String psRecordId, string psName, string psDescription){
        Product_Schedule__c psObj = new Product_Schedule__c();
        psObj.Invoice_Schedule__c=psRecordId;
        psObj.Product_Name__c=psName;
        psObj.Product_Description__c=psDescription;
 
        insert psObj;
        return psObj;
    }
}
If I add the id of my record page it works but if I am trying to pass the id through my js file it is not working.
Here is my JS File:
import {LightningElement, track, api } from 'lwc';
import submitSchedule from '@salesforce/apex/ScheduleClass.submitSchedule';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';

export default class ScheduleClass extends LightningElement {
    @track psObjName;
    @track psObjDescription;
    @api psRecordId;
    @track errorMsg;
   psHandleChange(event){
        if(event.target.name == 'psName'){
        this.psObjName = event.target.value;
        }
        if(event.target.name == 'psDescription'){
        this.psObjDescription = event.target.value;  
        }
 }
 submitAction(){
    submitSchedule({psRecordId:this.psRecordId,psName:this.psObjName,psDescription:this.psObjDescription})
    .then(result=>{
        this.psRecordId = result.Id;
        window.console.log('psRecordId ' + this.psRecordId);       
        const toastEvent = new ShowToastEvent({
            title:'Success!',
            message:'Record created successfully',
            variant:'success'
          });
          this.dispatchEvent(toastEvent);
          /*Start Navigation*/
          this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                psRecordId: this.psRecordId,
                objectApiName: 'Invoice_Schedule__c',
                actionName: 'view'
            },
         });
         /*End Navigation*/
    })
    .catch(error =>{
       this.errorMsg=error.message;
       window.console.log(this.error);
    });
 }
}

What Am I doing wrong?
Thanks
Eric
Eric Latreille 8Eric Latreille 8
I did replace psRecordId with recordId this was an error on my side but still not working.
SwethaSwetha (Salesforce Developers) 
HI Eric,
Do you see any information in the logs?
Nikolay MitjulNikolay Mitjul
Greetings people,

I also have issue with @api recordId;
I can see it in Salesforce Lightning, but do not see it in Experience site (community).
In Experience builder, property set to {!recordId}
Profile is Admin with all accesses.
Console log shows - "Undefined"
I did try to add this LWC on to different objects details page - Undefined.

I`m out of ideas.
If anyone has something to share, please share :)
Thank you in advance.

here are my simple code samples:

HTML
<template>
    <lightning-card title="Foto upload" icon-name="custom:custom19">
        Here you should see the Record Id {recordId}
        <lightning-button
            variant="brand"
            label="Console.log"
            title="Console.log"
            onclick={handleConsoleLogClick}
        ></lightning-button>
    </lightning-card>
</template>

JS
import { LightningElement, api, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class FileUploadLWC extends LightningElement {
    @api recordId;
    handleConsoleLogClick(event) {
        console.log(this.recordId);
    }
}

XML
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="recordComponent">
    <apiVersion>51.0</apiVersion>
    <description>externalUserFileUploader</description>
    <isExposed>true</isExposed>
    <targets>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
            <property name="recordId" type="String" label="Record ID"
                description="Should be set to {!recordId}"/>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

 
Nikolay MitjulNikolay Mitjul
Forgot to mention that simple Aura component in same EXP Site works perfectly.

Aura component
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="recordId" type="String" />
    {!v.recordId}
</aura:component>
Loran SagguLoran Saggu
Take a look at the documentation here (https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_record_context) - the recordId is not automatically bound in the Experience Site context. You need to bind the recordId in the xml file (see how the default value is set below):
<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>