【问题标题】:Catch exception in LWC thrown from Apex in Salesforce在 Salesforce 中的 Apex 中捕获 LWC 中的异常
【发布时间】:2021-07-19 19:05:10
【问题描述】:

我有下面的代码从 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 :");
      });
  }

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

【问题讨论】:

    标签: salesforce apex-code lwc


    【解决方案1】:

    正确的语法是.then().catch().finally(),而你写的是.then().error().finally()
    此外, associateAccount 返回void,因此不会从then 收到result。也没有理由将 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 中 Using Promises 的好读物

    【讨论】:

    • 谢谢鲁本。就是这样!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 2012-07-10
    相关资源
    最近更新 更多