[SalesForce] LWC Imperative Apex and the promise result

In Lightning web components, how can I process the results of an imperative Apex call? Specifically, I want to do some immediate processing if the Apex returns null. However, the result promise always seems to be undefined in JS so I can't proceed correctly.

In the example below, I know that findContacts is returning some records because the foundContacts displays correctly in a data-table on my page. However, the console log shows that result is always undefined, and so the processing occurs when it should not.

Is this because the promise is fulfilled asynchronously? How can I detect the fulfillment so that I can do the extra processing that I need?

Is there any documentation or a good example that shows how I can act on the results of the Apex call?

Javascript:

import { LightningElement, track, api, wire } from 'lwc';
import { createRecord, getRecord, getFieldValue, updateRecord } from 'lightning/uiRecordApi';
import findContacts from '@salesforce/apex/TMC_ContactServices.findContacts';

export default class BatchRegistration extends LightningElement {
    @track foundContacts;
    @track submittedContact = {};

    initialSubmit() {
        this.submittedContact = { 'sObjectType': 'Contact' };
        this.submittedContact.FirstName = this.template.querySelector('lightning-input[data-formfield="firstName"]').value;
        this.submittedContact.LastName = this.template.querySelector('lightning-input[data-formfield="lastName"]').value;
        this.submittedContact.Email = this.template.querySelector('lightning-input[data-formfield="email"]').value;

        // Call the Apex method to find existing contacts
        findContacts({ con: this.submittedContact })
            .then(result => {
                this.foundContacts = result;
                console.log('result: ' + this.result);
                console.log('result JSON: ' + JSON.stringify(this.result));
                /* PROBLEM: I want do more processing only when Apex returns an empty list, but result is always undefined */
                if (result === null || result === undefined) {
                    // Do some other actions
                }

            })
            .catch (error => {
                console.log('error ' + error.message);
            });
    }
}

Apex:

public without sharing class TMC_ContactServices {
    @AuraEnabled
    public static List<Contact> findContacts(Contact con) {
        List<Contact> foundInitialContacts = new List<Contact>();
        // I'm omitting a lot code here that finally returns a list of matching contacts
        ...
        return foundInitialContacts;
    }
}

Best Answer

console.log('result: ' + this.result); is undefined, because result is a local variable, not a class-level variable. You meant console.log('result: ' + result);, which refers to the variable in the local scope. That probably caused some of your confusion. Based on your Apex code, I presume you're getting an empty Array when there's no results, so:

if(!result.length) {
  // The list is empty, so do something here
}
Related Topic