2

I have a Lightning web component that is being called from a quick action custom button from a custom object record page. When the component is loaded, I need to retrieve the record so I can get a field value from it.

This answer says to use a wire with renderedCallback. I've tried it but the wire is not retrieving the record in the callback.

In my console logs, I see the correct recordId value in the callback, but the wired platoonEventis empty and the data is undefined:

in callback
recordId: "a1l0m000000IHxOAAW"
Platoon event: {}
Platoon event data: undefined

I've tried connectedCallback but that doesn't work either.

Is there an issue with the fact that this LWC is wrapped in an aura component because I'm using a quick action custom button?

Javascript:

import { LightningElement, track, api, wire } from 'lwc';
import { createRecord, getRecord, getFieldValue, updateRecord } from 'lightning/uiRecordApi';

import OCCASION_OBJECT from '@salesforce/schema/Occasion__c';
import OCCASION_PLATOONID from '@salesforce/schema/Occasion__c.Service_Platoon__c';

export default class BatchRegistration extends LightningElement {
    @api recordId;
    platoonId;

    // Get the platoon event record so we can get the platoon ID field.
    @wire(getRecord, { recordId: '$recordId', fields: [OCCASION_PLATOONID] })
    platoonEvent;

    /* Functions start here */

    // Initialize
    renderedCallback() {
        console.log('in callback');
        console.log('recordId: ' + JSON.stringify(this.recordId));
        console.log('Platoon event: ' + JSON.stringify(this.platoonEvent));
        console.log('Platoon event data: ' + JSON.stringify(this.platoonEvent.data));
        this.platoonId = getFieldValue(this.platoonEvent.data, OCCASION_PLATOONID);
    }

Note: I've also tried this but it doesn't work either:

@wire(getRecord, { recordId: '$recordId', fields: ['Occasion__c.Service_Platoon__c'] })
platoonEvent;
  • I am assuming your markup is blank. – Pranay Jaiswal Aug 29 at 19:22
  • @PranayJaiswal I don't use platoonEvent or platoonId in the markup, I want to use them for Apex parameters once a user clicks a button. – David Cheng Aug 29 at 20:11
2

From the documentation:

When a component rerenders, all the expressions used in the template are reevaluated. Due to mutations, a component is usually rendered many times during its lifespan in an application.

Now, as you are not using platoonEvent in the markup, the expression won't be evaluated, and the wire won't be called.

Fix: Use platoonEvent in the markup like

  <template>

    In my template

     Record Id is : {recordId}
    <br/>
    <template if:true={platoonEvent.data}>
        {platoonEvent.data.fields.Service_Platoon__c.value}
    </template>
</template>

Also the thing to remember is renderedCallback will be called multiple times, the first time its called, it won't have the value. You can see it from your debug logs

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.