+ Start a Discussion
SFDC-NOOBSFDC-NOOB 

Test Class Error

Greetings,

 

I can't seem to figure out why I am getting the following error.  I have the "Name" field in my test class and have confirmed it is not NULL using system.assertvalue.  What am I doing wrong?  Thank you in advance for your help!!!

 

 

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

Stack Trace::::::   Class.multirecords.save: line 20, column 1
                              Class.TestMultiRecords.TestMultiRecords: line 36, column 1

 

 

 

 

 

*************************************************************************TEST CLASS******************************************************

 

 

@istest
public class TestMultiRecords{
    
    public static testMethod void TestMultiRecords(){
    date mydate = date.today();
    string accountnametest = 'TestMultiRec';
    string opportunitynametest = 'testOpp';
    string firstnametest = 'John';
    string lastnametest = 'Doe';
        
        PageReference pageRef = Page.multirecords;
        Test.setCurrentPage(pageRef);
        
        multirecords Controller = new multirecords();
        
        account a = new account(Name = 'TestMultiRec',phone='4109011267',macro_industry__c='Construction',custom_account_source__c='Referral',preferred_mode_s__c='Van');
        insert a;
        system.assertequals(accountnametest, a.name);
        
        contact c = new contact(accountid=a.id,firstname='John',lastname='Doe');
        insert c;
        system.assertequals(firstnametest, c.firstname);
        system.assertequals(lastnametest, c.lastname);
        
        task t = new task(whoid=c.id,subject='Email',whatid=a.id,priority='Normal',Status='Not started');
        insert t;
        system.assertequals(c.id,t.whoid);
        
        opportunity o = new opportunity(accountid=a.id,name='testOpp',stagename='Discovered',closedate=mydate);
        insert o;
        system.assertequals(opportunitynametest,o.name);
           
        controller.getaccountlist();
        controller.getcontactlist();        
        controller.cancel();
        controller.save();          //Error here
        
    }
}

 

 

 

*****************************************************************Controller**********************************************************************

 

public class multirecords {

public Account A {get; set;}
Public Task T {get; set;}
Public Opportunity O {get; set;}
string inputstring{get;set;}


public multirecords()
{
A = new account();
T = new task();
O = new opportunity();
}


public PageReference save()
{

insert a;


T.whatid = A.id;
insert T;

O.Accountid = A.id;
insert O;

/* Redirects the page to the new account
*/

return new PageReference('/'+A.id);
}

public PageReference Cancel()
{
return new PageReference('/001/o');
}

/* Used for the Account list at the end of the visualforce page.
*/
public List<Account> getAccountList() {
return [select name, Type FROM account WHERE Owner.id =: userinfo.getuserid() AND Type != 'Disqualified' AND Type != 'Recycle' ORDER BY Name];
}

/* Used for the Contact list at the end of the
VisualForce page
*/

public List<Contact> getContactList() {
return [select name, title, email, phone FROM contact WHERE owner.id =: userinfo.getuserid()];
}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs
My apologies for the hurried response... You still have not set the account variable 'a' in the controller. Try adding this before calling the Save:
controller.a.name = 'TestAccount'

All Answers

vbsvbs
Your test class calls the controller cancel method first. This in turn navigates you to account home page. Hence the account variable state is lost and the insert on the save call fails. Try to imagine as you calling the cancel button on the UI and then trying to save which would not be possible.

As a fix try to call the save first and then the cancel method in the test class.
SFDC-NOOBSFDC-NOOB

Thank you for your reply!  I put the save method before the cancel method; however, I receive the same error. . .  Any other ideas?

vbsvbs
My apologies for the hurried response... You still have not set the account variable 'a' in the controller. Try adding this before calling the Save:
controller.a.name = 'TestAccount'
This was selected as the best answer
SFDC-NOOBSFDC-NOOB

That did the trick!!!  Thank you very much for your time and help!!