0

I have a component to show toast notifications on Salesforce. But the messages should prompt if the first method is undefined. I'm using async/await to try to execute this in order but it's not working.

The code:

    @wire(checkCustomerStatus, { accId: '$recordId'})
    async wiredcheckCustomerStatus  ({ error, data }){
        if(data) {
            this.message = data;
            this.custmStatus = data;
            this.variant = 'error',
            this.dispatchEvent(new CloseActionScreenEvent());
            this.showNotification();
        }
        if(error) {
            console.log('erro status -' + error);
        }
    }
    
    @wire(checkBlockingConditionsOB, { accId: '$recordId' })
    async wiredcheckBlockingConditionsOB ({ error, data }){

        await this.wiredcheckCustomerStatus();
        
        if(this.custmStatus != undefined) {
            
        }
        else {
            if(data) {
                this.message = data;
                this.variant = 'warning',
                this.dispatchEvent(new CloseActionScreenEvent());
                this.showNotification();  
            }
            if(error) {
                console.log('erro status -' + error);
            }
        }

At this point is my mistake:

await this.wiredcheckCustomerStatus();

I'm recieving at console this error:

oneSFA_PlaceOrderButton.js:4 Uncaught (in promise) TypeError: Cannot destructure property 'error' of 'undefined' as it is undefined.

I need that the second @wire block is executed only after the first @wire block.

Any tips?

Best Regards

1
  • Think there is other discussion on this subject on this forum. The wired mechanism is best avoided where calls need to be done in a specific order; imperative code can be written instead to e.g. make some requests in parallel and other requests sequentially using async/await/Promise.all/etc. For some cases you could fudge it using reactive properties (such as your '$recordId') but cleaner IMHO to use explicit Apex calls.
    – Keith C
    Commented Jan 26, 2022 at 17:18

2 Answers 2

2

There is no need of async/await. Once the first wired method returns data, you can call the second apex method imperatively:

@wire(checkCustomerStatus, { accId: '$recordId'})
wiredcheckCustomerStatus  ({ error, data }){
    if(data) {
        this.message = data;
        this.custmStatus = data;
        this.variant = 'error',
        this.dispatchEvent(new CloseActionScreenEvent());
        this.showNotification();
        
        checkBlockingConditionsOB({ accId: this.recordId })
            .then((result) => {
                this.message = result;
                this.variant = 'warning',
                this.dispatchEvent(new CloseActionScreenEvent());
                this.showNotification();  
            })
            .catch((error) => {
                console.log('erro status -' + error);
            });
    }
    if(error) {
        console.log('erro status -' + error);
    }
}
1
  • Thanks @RubenDG
    – barucco
    Commented Jan 26, 2022 at 20:43
0

@wire functions are executed by the lightning framework. You cannot influence the order of these calls so adding the async keyword there does nothing.

You can either in both functions check if the other one already returned and, if so, then show your toast, or like @RubenDG suggests call one (or both) of those functions directly instead of using @wire.

If you want things to respond as quickly as possible, you can create a setter for recordId and call both functions asynchronously from there.

Something like

    @api get recordId() {
        return this._recordId;
    }
    set recordId(value) {
        this.addAttribute('record-id', value);
        this._recordId = value;
        this.makeCalls();
    }

    async makeCalls() {
        let somevar = await checkBlockingConditionsOB({recordId: this._recordId});
        let othervar = await checkCustomerStatus({recordId: this._recordId});
        ...
        do things here
        ...
    }
1
  • Thanks @h-alves
    – barucco
    Commented Jan 26, 2022 at 20:44

You must log in to answer this question.

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