0

I'm new to LWC. I was trying to get the field values without calling the apex class. I am using Lightning Data Service i.e. getRecord. I'm not understanding why the @wire is not executing when the @wire is executed as I need some suggestion.

Below is the code: Aura Component which is called from Quick Action:

<aura:component implements="force:hasRecordId,force:lightningQuickAction">
    <c:uploadPo recordId="{!v.recordId}"></c:uploadPo> </aura:component>

LWC HTML:

<template>
    <p>
        {opportunityName}
    </p>
</template>

LWC JS:

import { LightningElement, api, wire, track } from 'lwc';

import { getRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

const FIELDS = [
    'Opportunity.Name',
];

export default class UploadPo extends LightningElement {
    @api recordId;

    @track Opportunity;
    @track opportunityName;

    connectedCallback(){
        console.log('recordId===>'+recordId);
    }

    @wire(getRecord, { recordId: '$recordId', fields : FIELDS })
    currentOpportunityValue({error,data}){
        if(error){
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error loading Order Value',
                    message: error.message,
                    variant: 'error',
                }),
            );
        }else if(data){
            console.log('data===>'+data);
            this.Opportunity = data;
            this.opportunityName = data.fields.Name.value;
        }
    }

}

LWC xml File:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>    
        <target>lightning__RecordPage</target>    
    </targets>
    <targetConfigs>    
        <targetConfig targets="lightning__RecordPage">    
            <property name="recordId" type="String" label="Record Id" description="Record Id"/>    
        </targetConfig>  
    </targetConfigs>
</LightningComponentBundle>

In the above code I am able to print only connectedCallback() console and @wired is not getting called. Thank you for your suggestions.

  • I use the same design and it works for me. The one thing I might try is adding a if true to the template for opportunityName or Opportunity - that might force something to rerender – BritishBoyinDC Feb 10 at 15:00
0

@api recordId is reserved for components available to record pages (it automatically gets assigned the right value).

Since the Lightning Web Component is not directly in a record page context, try another attribute name like opptyId.

I got this to work:

<aura:component implements="force:lightningQuickAction,force:hasRecordId">
  <c:sampleForm opptyId="{!v.recordId}"></c:sampleForm>
</aura:component>
<template>
  <p>Record ID: {opptyId}</p>
</template>
import { LightningElement, api } from 'lwc';

export default class SampleForm extends LightningElement {
  @api opptyId;
}
| improve this answer | |
  • Thanks for the response. I have tried using opportunityId but same result. I am going to add some other fields of opportunity for validation purpose not for UI rendering. Is it right way to do so. – lavesh galipelli Feb 11 at 5:40
  • I could make it work by simply renaming recordId in LWC. I'll update my answer. – POZ Feb 11 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.