【发布时间】: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