Salesforce Apex测试类在保存帐户时失败

时间:2016-05-05 21:37:42

标签: testing salesforce visualforce apex

弄清楚为什么我的测试类会返回System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]

我在SFDC中的Account对象上需要字段Name,但由于我使用Name属性模拟帐户不应该我的测试类保存功能不起作用吗?

以下是我的Apex课程 public with sharing class 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]

3 个答案:

答案 0 :(得分:0)

我在Salesforce中没有太多背景但我相信您需要在Test.startTest()之前插入您正在创建的对象;

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

答案 1 :(得分:0)

您正在尝试Id一个帐户。 Upsert(更新/插入)调用将在您要发起的帐户中查找Id。如果找到Upsert Insert可以正常工作Name。所以在你的情况下,它试图插入不更新。这就是为什么 <TextView android:layout_width="0dp" android:layout_height="match_parent" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Пн, сегодня" android:id="@+id/title" android:layout_weight="1" android:layout_gravity="center_vertical" android:gravity="center" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/icon" android:layout_weight="1" android:src="@drawable/ic_cloud_queue_24dp" android:layout_gravity="center_vertical" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:textAppearance="?android:attr/textAppearanceSmall" android:text="+17" android:id="@+id/genre" android:layout_weight="1" android:gravity="center" android:layout_gravity="center_vertical" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:textAppearance="?android:attr/textAppearanceSmall" android:text="+9" android:id="@+id/year" android:layout_weight="1" android:gravity="center" android:layout_gravity="center_vertical" /> </LinearLayout> 是强制性的。

答案 2 :(得分:0)

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

@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();
 }
}