【问题标题】:Salesforce Apex Test Class Failing on saving of an accountSalesforce Apex 测试类在保存帐户时失败
【发布时间】:2016-05-10 09:51:55
【问题描述】:

在弄清楚为什么我的测试类返回 System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name] 时遇到了一些麻烦

我在 SFDC 的 Account 对象上有必填字段 Name,但是由于我使用 Name 属性模拟 Account,我的测试类保存功能不应该工作吗?

下面是我的 Apex 课程 public 与共享类 QuoteAccountController {

// Define VariableType and VariableName
public ApexPages.StandardController standardContactController;
public Account Account{get;set;}
public Contact Contact{get;set;}
public Account selectedAccount{get;set;}
public Boolean displayProjectInformation{get;set;}
public Boolean projectNameError{get;set;}
public Boolean projectValidationsPassed{get;set;}

//Page Constructor/Initializer
public QuoteAccountController(ApexPages.StandardController StandardController) {
    Account = new Account();    
    Contact = new Contact();
    displayProjectInformation = true;
    projectNameError = false;
    projectValidationsPassed = true;
}

public pageReference save() {
    projectValidations();
    if (projectValidationsPassed) {
        upsert Account;
        Contact.accountId = Account.id;
        upsert Contact;
        Contact = new Contact();
        Account = new Account();
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
        return null;
     } else {
         return null;
     }
}

public void projectValidations(){
    if (Account.Subscription_Type__c == 'Project' && String.isBlank(Account.Project_Name__c)) {
        projectValidationsPassed = false;
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Project Name is required field'));
    } else if (Account.Subscription_Type__c == 'Project' && String.isBlank(Account.Project_Type__c)) {
        projectValidationsPassed = false;
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Project Type is required field'));
    } else if (Account.Subscription_Type__c == 'Project' && Account.Project_Start_Date__c == null) {
        projectValidationsPassed = false;
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Project Start Date is required field'));
    } else if (Account.Subscription_Type__c == 'Project' && Account.Project_End_Date__c == null){
        projectValidationsPassed = false;
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Project End Date is required field'));
    } else {
        projectValidationsPassed = true;
    }
}  

下面是我的 Apex 测试课

@isTest
public class QuoteAccountControllerTest {
 private static testmethod void testSave() {
     Quote quote = new Quote();
     Account testAcc = new Account();
     Contact con = new Contact();
     ApexPages.StandardController stdCont = new ApexPages.StandardController(testAcc);
     QuoteAccountController quoteAccCont = new QuoteAccountController(stdCont);
     PageReference page = new PageReference('/apex/zqu_QuoteAccount?quoteType=Subscription&stepNumber=1');
     Test.setCurrentPage(page);

     testAcc.Project_Name__c = 'Project Name';
     testAcc.Name = 'Test Account';
     testAcc.Project_Start_Date__c = Date.today();
     testAcc.Project_End_Date__c = Date.today().addDays(2);
     testAcc.Project_Type__c =  'Convention Center';
     testAcc.Region__c = 'US';
     testAcc.Subscription_Type__c = 'User';

     Test.startTest();
     quoteAccCont.save();
     Test.stopTest();
 }
}

谢谢!

编辑:下面的错误消息。

System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]

【问题讨论】:

  • 请填写完整的错误行号。
  • @AjaySainy 添加了错误消息。谢谢!

标签: testing salesforce visualforce apex


【解决方案1】:

我在 Salesforce 方面没有太多背景,但我认为您需要在 Test.startTest(); 之前插入您正在使用 new 创建的对象;

如果这也失败了,请尝试在 Apex 测试类中使用 System.debug() 并使用 Salesforce 开发人员控制台及其日志来跟踪您的错误。也许这有帮助。祝你好运!

【讨论】:

  • 谢谢乔迪。我正在尝试第一次测试帐户的创建,在这种情况下将插入,所以我认为我不应该插入然后运行#save
  • 不客气。就我而言,在同事的帮助下,我只完成了 3 个测试课程。您是否尝试通过查询获取贵组织的一些帐户?这可能是另一种覆盖您的课程的方式,并且您将避免丢失字段问题。然后,一旦你得到它,你应该能够对其进行更新。
【解决方案2】:

您正在尝试upsert 一个帐户。 Upsert(Update/Insert) 调用将在您正在更新的帐户中查找Id。如果找到Id Upsert 会正常工作,否则它会Insert。所以在你的情况下,它试图插入而不是更新。这就是为什么Name 是强制性的。

【讨论】:

  • 感谢 Ajay 审核我的请求。在我的测试类中,我为我要保存的帐户提供了一个名称属性,所以必填字段不应该通过吗? testAcc.Name = '测试帐号';
  • 马特,你有没有试过调用保存方法。我希望它仍然会抛出错误。问题不在测试课上。由于您在实际类中创建数据而不是查询数据,因此它不会引用您在测试类中创建的记录。
【解决方案3】:

想出了解决办法。需要将帐户和联系人分配给控制器。请参阅下面的修改代码。

@isTest
public class QuoteAccountControllerTest {
 private static testmethod void testSave() {
 Quote quote = new Quote();
 ApexPages.StandardController stdCont = new ApexPages.StandardController(testAcc);
 QuoteAccountController quoteAccCont = new QuoteAccountController(stdCont);
 quoteAccCont.Account = new Account(ATTRIBUTES_HERE);
 quoteAccCont.Contact = new Contact(ATTRIBUTES_HERE
 PageReference page = new PageReference('/apex/zqu_QuoteAccount?quoteType=Subscription&stepNumber=1');
 Test.setCurrentPage(page);

 Test.startTest();
 quoteAccCont.save();
 Test.stopTest();
 }
} 

【讨论】:

    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    相关资源
    最近更新 更多