You need to sign in to do that
Don't have an account?
etechcareers
Help with Wizard Testclass
Hi all:
I am using the wizard apec controller from the book, and decided to write a test class I am getting errors all over ...
please help..
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]
Class.newOpportunityController.save: line 88, column 7 Class.newOpportunityController.hol: line 146, column 8 External entry point
public class newOpportunityController {
// These four member variables maintain the state of the wizard.
// When users enter data into the wizard, their input is stored
// in these variables.
Account account;
Contact contact;
Opportunity opportunity;
OpportunityContactRole role;
// The next four methods return one of each of the four member
// variables. If this is the first time the method is called,
// it creates an empty record for the variable.
public Account getAccount() {
if(account == null) account = new Account();
return account;
}
public Contact getContact() {
if(contact == null) contact = new Contact();
return contact;
}
public Opportunity getOpportunity() {
if(opportunity == null) opportunity = new Opportunity();
return opportunity;
}
public OpportunityContactRole getRole() {
if(role == null) role = new OpportunityContactRole();
return role;
}
// The next three methods control navigation through
// the wizard. Each returns a PageReference for one of the three pages
// in the wizard. Note that the redirect attribute does not need to
// be set on the PageReference because the URL does not need to change
// when users move from page to page.
public PageReference step1() {
return Page.opptyStep1;
}
public PageReference step2() {
return Page.opptyStep2;
}
public PageReference step3() {
return Page.opptyStep3;
}
// This method cancels the wizard, and returns the user to the
// Opportunities tab
public PageReference cancel() {
PageReference opportunityPage = new ApexPages.StandardController(opportunity).view();
opportunityPage.setRedirect(true);
return opportunityPage;
}
// This method performs the final save for all four objects, and
// then navigates the user to the detail page for the new
// opportunity.
public PageReference save() {
// Create the account. Before inserting, copy the contact's
// phone number into the account phone number field.
account.phone = contact.phone;
insert account;
// Create the contact. Before inserting, use the id field
// that's created once the account is inserted to create
// the relationship between the contact and the account.
contact.accountId = account.id;
insert contact;
// Create the opportunity. Before inserting, create
// another relationship with the account.
opportunity.accountId = account.id;
insert opportunity;
// Create the junction contact role between the opportunity
// and the contact.
role.opportunityId = opportunity.id;
role.contactId = contact.id;
insert role;
// Finally, send the user to the detail page for
// the new opportunity.
PageReference opptyPage = new ApexPages.StandardController(opportunity).view();
opptyPage.setRedirect(true);
return opptyPage;
}
static testmethod void hol(){
newOpportunityController ext = new newOpportunityController();
Account acct = new Account();
acct.Name = 'sjsjs';
insert acct;
ext.getAccount();
Contact con = new Contact();
con.firstname='jdhjd';
con.lastname='hdjdhdh';
con.email='hello@email.com';
insert con;
ext.getContact();
Opportunity opp = new Opportunity();
opp.Name = 'lslsls';
opp.Accountid=acct.id;
opp.stageName='Lead';
opp.CloseDate=Date.newinstance(2010,12,31);
insert opp;
ext.getOpportunity();
ext.save();
}
}
you cannot create a contact without an Account. So in the test method.
just added the field account in contacts and it will work fine.