1

I'm trying to pass the recordId to the controller so I can interact with the person account to send the data. The problem I found was that recordId was not sending its data correctly to the controller. So I did console.log recordId and it was giving me an undefined. Any idea of getting recordId?

.js

      @api recordId;

      async connectedCallback() {
        console.log(this.recordId) // this will give me undefined
        uploadImage({
          fileName: 'image.png',
          base64Data: image_data_url,
          recordId: this.recordId,
        })
      }

     photoShot() {
        canvas.getContext("2d").drawImage(video, 0, 0, canvas.width, canvas.height); // draw html5 canvas
        const image_data_url = canvas.toDataURL("image/jpeg").replace("data:image/jpeg;base64,", "");
     }

.cls

        @AuraEnabled
        public static string uploadImage(String fileName, String base64Data, Id recordId) {
            // Decoding the base64Data
            base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');
            ContentVersion cv = new ContentVersion();
            cv.VersionData = EncodingUtil.base64Decode(base64Data);
            cv.Title = fileName;
            cv.PathOnClient = '/' + fileName;
            cv.IsMajorVersion = true;
            cv.FirstPublishLocationId = recordId;
            Insert cv;

            ContentDocumentLink cdl = new ContentDocumentLink();
            cdl.ContentDocumentId = [
                  SELECT ContentDocumentId 
                  FROM ContentVersion 
                  WHERE Id =: cv.Id
                ].ContentDocumentId;
            cdl.LinkedEntityId = recordId;
            cdl.ShareType = 'V';
            cdl.Visibility = 'AllUsers';
            insert cdl;
            System.debug('ContentDocumentLink');
            System.debug(cdl.Id);
        }

.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <LightningComponentBundle 
    xmlns="http://soap.sforce.com/2006/04/metadata">
        <apiVersion>54.0</apiVersion>
        <isExposed>true</isExposed>
        <targets>
            <target>lightning__AppPage</target>
            <target>lightning__RecordPage</target>
            <target>lightning__HomePage</target>
            <target>lightningCommunity__Page</target>
            <target>lightningCommunity__Default</target>
        </targets>
        <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>
    </LightningComponentBundle>
1
  • 2
    As suggested in the answer, check the context of use. Is this in a lightning record page or appropriate digital experience page that has a mapping for the recordId in its URL?
    – Phil W
    Commented Jun 7, 2022 at 17:22

1 Answer 1

2

To access the record id you need to add the component to a Lightning record page.
You need to also import the @api like below:

import { LightningElement, api } from 'lwc';

Please note that

"The recordId is 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 can’t depend on it."

If your component is added on an Expereince Site then you need to add the following in your meta file:

<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>

For your reference please refer to the following Link

9
  • 2
    There is always the case where the recordId is explicitly set by a parent component on this child component...
    – Phil W
    Commented Jun 7, 2022 at 17:21
  • 1
    @Krishnan Mishra Hey thanks for the help. I put a new code there for the xml file. Take a look there, It is still giving me undefined on console.log(this.recordId).
    – Ian Lee
    Commented Jun 7, 2022 at 17:57
  • @IanLee Can you please specify where are you using this component? Commented Jun 7, 2022 at 18:01
  • Okay, I'm getting base64 data from a camera and trying to send that data to person account(file). but in order to do that I need to use controller with pass in parameter(recordId, base64, fileName). I can pass in base64 and fileName but the recordId. I have set the xml just now but still not getting the initial value from console.log(this.recordId) which makes me to assume that It's still not getting the recordId.
    – Ian Lee
    Commented Jun 7, 2022 at 18:12
  • 1
    @IanLee Your custom lwc component must be rendered on a Lightning record page then only you can use the native recordId. What I was asking you was where(like Record Detail page, community) is your lwc component being rendered? Commented Jun 7, 2022 at 18:19

You must log in to answer this question.

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