【问题标题】:I need to create a new expense card我需要创建一张新的费用卡
【发布时间】:2022-11-11 06:53:10
【问题描述】:

我需要创建一张新的消费卡,输入参数是金额、日期和描述。

创建费用卡必须填写的字段: 持卡人是联系方式 Months Expenses Applications 是一个自定义对象

创建费用卡时,如果在“日期”字段中输入的日期之前存在 Months Expenses Applications,则会创建一个新的费用卡,从现有的 Months Expenses Applications 中获取

如果在“日期”字段中输入的日期之前没有月份费用应用程序,您需要创建月份费用应用程序,然后创建一个费用地图,其中月份费用应用程序将创建一个新的月份费用应用程序

我试图创建一个“金额”“日期”“描述”等于输入参数的费用地图,但我不知道如何指定 MonthExpenseApplication__c

public static void createNewExpenseCard(Integer amount, Date createdDate, String description) {
        
        Month_Expense_Application__c MonthApplication = [
            SELECT Name, MonthDate__c
            FROM Month_Expense_Application__c
            WHERE MonthDate__c =: createdDate
        ];
        if (MonthApplication != null) {
            ExpenseCard__c exp = new ExpenseCard__c(
                Amount__c = amount,
                CardDate__c = createdDate,
                Description__c = description,
                CardKeeper__c = '0034x00001K7kGCAAZ'
            );
            exp.MonthExpenseApplication__c = [
                SELECT MonthExpenseApplication__c
                FROM ExpenseCard__c 
                WHERE MonthExpenseApplication__c =: MonthApplication.Id
            ].Id;
            insert exp;
        } else {
            Month_Expense_Application__c monthApp = new Month_Expense_Application__c(
                Balance__c = 1000,
                MonthDate__c = createdDate,
                Keeper__c = '0034x00001K7kGCAAZ'
            );
            ExpenseCard__c exp2 = new ExpenseCard__c(
                Amount__c = amount,
                CardDate__c = createdDate,
                Description__c = description,
                CardKeeper__c = '0034x00001K7kGCAAZ'
            );
            
            insert exp2;
        }
        
    }

【问题讨论】:

    标签: salesforce apex lwc


    【解决方案1】:

    这是危险的:

    Month_Expense_Application__c MonthApplication = [
                SELECT Name, MonthDate__c
                FROM Month_Expense_Application__c
                WHERE MonthDate__c =: createdDate
            ];
    

    如果结果为零,它将抛出“list has no rows for assignment”。如果超过 1 则类似。

    像这样的东西?

    Integer amount = 50;
    Date createdDate = System.today();
    String description = 'Lorem ipsum...';
    
    Month_Expense_Application__c app;
    
    List<Month_Expense_Application__c> applications = [SELECT Id
        FROM Month_Expense_Application__c
        WHERE MonthDate__c =: createdDate
        LIMIT 1];
    
    if(applications.isEmpty()){
       app = new Month_Expense_Application__c(
          Balance__c = 1000,
          MonthDate__c = createdDate,
          Keeper__c = '0034x00001K7kGCAAZ'
       );
       insert app;
    } else {
       app = applications[0];
    }
    
    // one way or another - the monthly allowance exists now. So just use its id in the lookup
    
    ExpenseCard__c exp = new ExpenseCard__c(
       Amount__c = amount,
       CardDate__c = createdDate,
       Description__c = description,
       CardKeeper__c = '0034x00001K7kGCAAZ',
       Month_Expense_Application__c = app.Id
    );
    

    有更优雅的方法可以做到这一点,您需要阅读有关 upsert 和外部 id 的信息 - 但它应该足够好。

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      相关资源
      最近更新 更多