2

I would need the value of a field from the current record to pass along to make an Continuation API callout within the connectedCallBack( ). But, the field value I am trying to access via @wire is getting executed upon completion of the connectedCallBack( ). Sample code below:

import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import REVENUE_FIELD from '@salesforce/schema/Account.AnnualRevenue';

const fields = [REVENUE_FIELD, CREATED_FIELD, EXP_FIELD];

export default class WireGetValue extends LightningElement {
@api recordId;
revData; 

@wire(getRecord, { recordId: '$recordId', fields })
account;

get revenue() {
    return getFieldValue(this.account.data, REVENUE_FIELD);
}

connectedCallback(){
    this.revData = this.revenue(); //How can I get this executed upon getting data?
    console.log('NEW: ' +this.revData); //Getting as 'undefined'
    this.makeAPICallout(this.revData); //Failing.
}   
}

This LWC would be used as a 'Quick Action' button on the record detail page.

Referred to these already:

SFSE solution #1

SFSE solution #2

And many others, but my scenario includes calling an API within the connectedCallBack( ). Is there a way I can access this specific field value without* involving Apex here?

1 Answer 1

2

As I've explained before, no wire methods can fire before connectedCallback. That's because the framework is atomic and synchronous in the rendering cycle. The best you could try to do would be to make connectedCallback asynchronous, but that has the drawback that you have no idea how long LDS (Lightning Data Service) will take to provision the record. In other words, the following code may work:

async connectedCallback() {
  await Promise.resolve();
  const revData = this.revenue;
  this.makeAPICallout(revData);
}

If the record is in the cache (for a quick action, this is likely true), you should see the data immediately, as the data is already in LDS cache. The downside, however, is that in LEX, components may or may not be connected or disconnected from the DOM when you expect. It's entirely possible you'll have the wrong record if users navigate around, or you won't get the record at the right time anyways.

For a Quick Action, use the invoke() method. This will always have the correct data:

invoke() {
  this.makeAPICallout(this.revenue);
}

However, it will only fire when the user actually clicks on the Quick Action, which is how Quick Actions are meant to be used. You shouldn't necessarily try to perform an action the instant a component loads.

Alternatively, if you really mean to access the data the instant it is available, use a wire handler:

revData;
@wire(getRecord, { recordId: '$recordId', fields }) handleRevData(response) {
  this.revData = response;
  if(response.data) {
    this.makeAPICallout(this.revenue);
  }
}

This allows you to instantly respond to initial loads as well as any time an edit is detected by LDS. There's other ways you could probably make this work, but the main point is that the most direct solution you're looking for simply isn't possible.

P.S. getters are not called as functions, they're called as properties.

Correct:

const revenue = this.revenue;

Incorrect:

const revenue = this.revenue();

You must log in to answer this question.

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