Wednesday, 20 October 2021

How to get record-Id in LWC screen action ?

 If you have created quick action and referred LWC component, you will get "Undefined" as Record Id in ConnectedCallback method, This is the issue I have come across. 

connectedCallback() {
        console.log('connected===============');
        console.log(this.recordId); // This will print undefined.
}

Solutions - 1: We can wrap LWC component with aura, and pass record Id.
<aura:component implements="force:lightningQuickActionWithoutHeader,
force:hasRecordId,force:appHostable>
   <c:updateFiles recordId="{!v.recordId}"/>
</aura:component>

Solution - 2 Use RecordId in component html code.
<div style="display:none">
    {recordId}
 </div>

Component Controller
import { LightningElement,api,track } from 'lwc';
import { CloseActionScreenEvent } from 'lightning/actions';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class UpdateFiles extends LightningElement {
    @api recordId;
   
    connectedCallback() {
        console.log('connected===============');
        console.log(this.recordId);
        
    }
}

Component Code
<template>
    <lightning-quick-action-panel header="Mark Files To Be Deleted">
        <div style="display:none">
            {recordId}
        </div>
        <div slot="footer" class="slds-modal__footer">
     <lightning-button variant="destructive" label="Cancel"></lightning-button>
  <lightning-button class="slds-m-left_x-small" variant="brand" 
label="Delete Files"  onclick={getSelectedRec>
</lightning-button>
            
          </div>
    </lightning-quick-action-panel>
</template>

XML code
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
  <target>lightning__RecordAction</target>
</targets>
<targetConfigs>
  <targetConfig targets="lightning__RecordAction">
    <actionType>ScreenAction</actionType>
  </targetConfig>
</targetConfigs>
</LightningComponentBundle>


No comments: