1

LWC Component question. I have an Id field:

export default class CustomerQuoteAcceptDecline extends LightningElement {

_quoteId;  // record Id

which I populate from URL parameters in the connectedCallback():

    connectedCallback() {

    let parmMap = LightningUtil.getURLParameters();

    LightningUtil.log('parmMap size:' + parmMap.size);
    LightningUtil.log('HASH_PARM:' + parmMap.get(this.HASH_PARM));
    LightningUtil.log('QUOTE_PARM:' + parmMap.get(this.QUOTE_PARM));
    LightningUtil.log('ACTION_PARM:' + parmMap.get(this.ACTION_PARM));

    if(parmMap.size > 0) {            

        if(parmMap.has(this.HASH_PARM)) {
            this._loadHash = parmMap.get(this.HASH_PARM);
        }
        if(parmMap.has(this.QUOTE_PARM)) {
            this._quoteId = parmMap.get(this.QUOTE_PARM);
        }
        if(parmMap.has(this.ACTION_PARM)) {
            let action = parmMap.get(this.ACTION_PARM);
            if(action == this.ACCEPT_TENDER) {
                this._isAccept = true;
                this._isDecline = false;
            }
            else {
                this._isAccept = false;
                this._isDecline = true;
            }
        }
    }
}

I see the parameters populated in the console no problem.

However this @wire function fails:

    @wire(getRecord, { recordId: '$_quoteId', fields: [QUOTE_ID_FIELD, LOAD_FIELD]})
wiredQuote({ error, data }) {

    if(data) {
        this._quote = data.fields;
        this._loadId = this._quote[LOAD_FIELD.fieldApiName].value;
        LightningUtil.log('wiredQuote Success');
    }
    else if (error) {
        LightningUtil.logError(this, error);
        this._isError = true;
        LightningUtil.log('wiredQuote Fail');
    }
}

I see "wiredQuote Fail" in the console.

Any idea what is going on here? Is the @wire jumping the gun before the _quoteId parameter is populated by connectedCallback()? Or is reactivity somehow goofing it up? Do @wire functions invoke (too soon) when the reactive parameter is (still) null?

Thanks for your help!

  • 1
    There is a weird problem with parameters starting with _ - faced before. Can you try removing underscore? – salesforce-sas Jun 24 at 22:49
  • 1
    I ran your code and it seems to be working alright for me even though I have the _ in place in the name of _quoteId. I also tried adding a timeout function of 1500ms before setting the _quoteId and the wire responds perfectly as soon as the _quoteId value gets populated. What is the error that you are getting when the wire fails? Is it saying that _quoteId is undefined? – Arthlete Jun 25 at 3:51
  • Thanks folks for trying to help. I get "The requested resource does not exist...." in the console. I wonder if it has anything to do with the fact that I pass the 15-character ID in as _quoteId not the 18-character Id.... – Dave C Jun 25 at 12:32
  • @DaveC That should not matter. Can you load the record in Salesforce for that id? Is it a valid Salesforce Id? Maybe the record was deleted? Or perhaps it's a permissions issue for the user running the query? – Arthlete Jun 25 at 15:14
0

In lwc, @wire called before the connected callback(),

so set the _quoteId value in the constructor()

so, below code will work:

export default class CustomerQuoteAcceptDecline extends LightningElement {
@api _quoteId;  // record Id

constructor() {
    super();
    let parmMap = LightningUtil.getURLParameters();

    LightningUtil.log('parmMap size:' + parmMap.size);
    LightningUtil.log('HASH_PARM:' + parmMap.get(this.HASH_PARM));
    LightningUtil.log('QUOTE_PARM:' + parmMap.get(this.QUOTE_PARM));
    LightningUtil.log('ACTION_PARM:' + parmMap.get(this.ACTION_PARM));

    if(parmMap.size > 0) {            

        if(parmMap.has(this.HASH_PARM)) {
            this._loadHash = parmMap.get(this.HASH_PARM);
        }
        if(parmMap.has(this.QUOTE_PARM)) {
            this._quoteId = parmMap.get(this.QUOTE_PARM);
        }
        if(parmMap.has(this.ACTION_PARM)) {
            let action = parmMap.get(this.ACTION_PARM);
            if(action == this.ACCEPT_TENDER) {
                this._isAccept = true;
                this._isDecline = false;
            }
            else {
                this._isAccept = false;
                this._isDecline = true;
             }
         }
     }
 }
| improve this answer | |
  • That does not matter. As per documentation: "If a reactive variable changes, the wire service provisions new data." Meaning that as soon as connectedCallback() finishes running and sets the _quoteId variable the wired method will run again. – Arthlete Jun 25 at 8:24
  • But does the @wire run initially when the parameter is null? That would bomb out... – Dave C Jun 25 at 12:32
  • @DaveC it runs initially yes, and then it runs again once the value gets set from the connectedCallback – Arthlete Jun 25 at 15:06
  • Karthiba -- Thanks you were correct! I moved the parm reading to the constructor and am good to go. Thanks again. – Dave C Jun 25 at 18:40
  • 1
    Arthlete -- Thanks for your help also! – Dave C Jun 25 at 18:40

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.