0
 handleAddRows(event) {
        if (this.selectedSubjTemplate.length > 0) {
            this.isLoadingModal = true;
            createRecord({ quoteId: this.quoteRec, subjTemplates: this.selectedSubjTemplate })
                .then(operation => {
                    if(operation ==  'success'){
                        this.isLoadingModal = false;
                        this.showToast("Success", "Added successfully!", "success");
                        //console.log('@@Rows added::'+result);
                        this.closeModal();        
                    }
                })
                .catch(error => {
                    console.log('## error in creating records: ' + JSON.stringify(error));
                    this.showToast("Error", "Error in creating record!", "error");
                })
        }

Despite receiving a success response from Apex method, the promise is executing the catch block.

3
  • Please make sure to use the tools in the editor to format your code. Thanks!
    – David Reed
    Commented Feb 6, 2021 at 21:18
  • Sure David. Any idea about the issue?
    – VChandra
    Commented Feb 6, 2021 at 21:21
  • could you also share the error thats getting printed by the catch block Commented Feb 6, 2021 at 21:49

1 Answer 1

5

If an error occurs at any point in the then handler, this will cause the catch block to execute. That means if there's a problem with showToast or closeModal, you'll get to the catch block all the same. To understand why, consider the alternative form of your code:

async handleAddRows(event) {
    if (this.selectedSubjTemplate.length > 0) {
        this.isLoadingModal = true;
        try {
            const operation = await createRecord({ quoteId: this.quoteRec, subjTemplates: this.selectedSubjTemplate });
            if (operation == 'success') {
                this.isLoadingModal = false;
                this.showToast("Success", "Added successfully!", "success");
                //console.log('@@Rows added::'+result);
                this.closeModal();
            }
        } catch (error) {
            console.log('## error in creating records: ' + JSON.stringify(error));
            this.showToast("Error", "Error in creating record!", "error");
        }
    }
}

As you can see, even with a successful return, if there's a problem further down the line, you'll still hit the same exception handler (this has tripped me up more than once, to be sure).

Make sure that you're testing your code for any exceptions, such as undefined variables, accessing null objects, etc. You might consider using finally to close the modal, or something else. Try to avoid doing any heavy logic in the then handler that might throw errors.

You can read more about this behavior here.

3
  • Worth a link to something like this?
    – Phil W
    Commented Feb 6, 2021 at 21:44
  • Thank you! @sfdcfox showToast function was the culprit here.
    – VChandra
    Commented Feb 6, 2021 at 21:50
  • 1
    @PhilW Definitely. Added it in for future visitors.
    – sfdcfox
    Commented Feb 7, 2021 at 3:41

You must log in to answer this question.

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