首页 > 解决方案 > 在 Salesforce 中的 Apex 中捕获 LWC 中的异常

问题描述

我有下面的代码从 Apex 抛出异常

@AuraEnabled()
public static void associateAccount(string userId, string accountSAPNumber) {

    if(String.isBlank(userId) || string.isBlank(accountSAPNumber)) {            
        throw new AuraHandledException('Please specify both User Email2 and SAP Number2');
    }

    List<user> users = [SELECT Id, Name,Email FROM User WHERE Email  =: userId AND IsActive = true AND Profile.Name ='OEA Customer'];
    
    if(users == null || users.size() <= 0) {
        NoDataFoundException noUsersFound = new NoDataFoundException();
        noUsersFound.setMessage('No users found with the specified email address: ' + userId);
        throw noUsersFound;
    }

    Id accountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('OEA Customer Location').getRecordTypeId();
    accountSAPNumber = '%' + accountSAPNumber; 
    List<Account> accounts = [SELECT Id FROM Account WHERE RecordTypeId =:accountRecordTypeId AND SAP_Number__c like :accountSAPNumber];

    if(accounts == null || accounts.size() <= 0) {
        NoDataFoundException noAccountFound = new NoDataFoundException();
        noAccountFound.setMessage('No accounts found with the specified SAP Number: ' + accountSAPNumber);
        throw noAccountFound;
    }
    else if(accounts.size() > 1) {
        SearchException moreThan1Account = new SearchException();
        moreThan1Account.setMessage('More than 1 account found with the specified SAP Number: ' + accountSAPNumber);
        throw moreThan1Account;
    }

    OEA_NewContactFormController.accountMethod(userId, accountSAPNumber);       
}

我无法使用以下方法在我的 LWC 控制器中捕获此异常

continueButtonClicked() {
    associateAccount({
      userId: this.searchKey,
      accountSAPNumber: this.accountNumber,
    })
      .then((result) => {
        try {
          console.log("return from remote call " + result);
          this.modalPopUpToggleFlag = false;
        } catch (error) {
            console.log('some error');
        }
      })
      .error((error) => {
        console.log("some error in code");
        /*if (Array.isArray(error.body)) {
          console.log(
            "error message :" + error.body.map((e) => e.message).join(", ")
          );
        } else if (typeof error.body.message === "string") {*/
        //console.log("error message :" + error);
        //}
      })
      .finally(() => {
        console.log("finally message :");
      });
  }

这总是在控制台上给我一个错误,如“未捕获(承诺中)”以及异常的详细信息。如何以处理的方式捕获此异常。

标签: salesforceapex-codelwc

解决方案


正确的语法是.then().catch().finally(),当你写的时候.then().error().finally()
而且 associateAccount 返回void,所以不会有result收到 from then。也没有理由包含this.modalPopUpToggleFlag = false;在 try-catch 中,只有当您从未定义过modalPopUpToggleFlag可能有错误时。

continueButtonClicked() {
    associateAccount({
        userId: this.searchKey,
        accountSAPNumber: this.accountNumber,
    })
    .then(() => {
        console.log("return from remote call);
        this.modalPopUpToggleFlag = false;
    })
    .catch((error) => {
        console.log("some error in code:", error);
    });
}

这是一篇关于在 Javascript 中使用 Promises的好读物


推荐阅读