function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Maf_007Maf_007 

Visual force Test Class Code error

Hi All,

 

I am new in visual force. I have written my first visual force controller class and having issue with the test class. My controller class is below:

 

public class TestVFController{
    Private Account ACID;
    public List<Contact> Conts {get; set;}
    public Boolean refreshPage {get; set;}
    ApexPages.StandardController stdCtrl;

public TestVFController(ApexPages.StandardController myController) {
    	stdCtrl = myController;
    	refreshPage = false;
        This.ACID = (Account)stdCtrl.getRecord();
        Conts = new List<Contact>();
        Contact c = new Contact();
        c.accountid = ACID.id;
        Conts.add(c);
}
        
    public void addrow() {
        Conts.add(new Contact());}
            
    public void removerow(){
        Integer i = conts.size();
        Conts.remove(i-1);}
            
    public PageReference savereturn() {
        insert Conts;
        //PageReference result = stdctrl.view();
        PageReference result = new PageReference('/apex/test2');
        //PageReference result=ApexPages.currentpage();
        result.setRedirect(true);
        return result;
    }
        
    public PageReference OK(){
        refreshPage = true;
        PageReference pageRef = new PageReference('/apex/test1');
        //pageRef.setRedirect(true);
        return pageref;
    }
}

 

And my test class is below:

@isTest
public class TestForController{
    static testMethod void Testmycontroller() {
        List<Contact> c = new List<Contact>{};
        Account act = new Account(Name = 'Test acc');
        insert act;
        
        PageReference pageRef = Page.test1;
        Test.setCurrentPage(pageRef);
        
        // Instantiate the standard controller
        Apexpages.StandardController sc = new Apexpages.standardController(act);
        TestVFController mycon  = new TestVFController(sc);
        String nextPage = mycon.savereturn().getUrl();
        
        Contact con = new Contact();
        con.FirstName = 'Maf';
        con.LastName = 'Test';
        Con.Email = 'maf@test.com';
        Con.accountid = act.id;
        c.add(Con);
        insert c;
        
        mycon.Conts.add(con);
        
        mycon.savereturn();
        mycon.addrow();
        mycon.removerow();
    }
}

 

Note that, My Standard Controller is Account and My extension controller class is Adding multiple contacts related to that account

 

I get the following error and only 53% code coverage:

 

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


Class.TestVFController.savereturn: line 25, column 1
Class.TestForController.Testmycontroller: line 15, column 1

 

Could anyone please help me on that???

 

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

change below lines, no need to give last name in controller.

 

public TestVFController(ApexPages.StandardController myController) {
    	stdCtrl = myController;
    	refreshPage = false;
        This.ACID = (Account)stdCtrl.getRecord();
        Conts = new List<Contact>();
if(Test.isRunningtest()==false){ Contact c = new Contact();
c.LastName = LastName;
c.FirstName = FirstName; c.accountid = ACID.id; Conts.add(c);
} }

 

All Answers

Dhaval PanchalDhaval Panchal

see below lines in your apex class.

 

public TestVFController(ApexPages.StandardController myController) {
    	stdCtrl = myController;
    	refreshPage = false;
        This.ACID = (Account)stdCtrl.getRecord();
        Conts = new List<Contact>();
        Contact c = new Contact();
        c.accountid = ACID.id;
        Conts.add(c);
}

 Here value for field "lastname" is missing which is mandatory field of contact object. By default you are adding one contact record to your list "Conts".

Maf_007Maf_007

Hi,

 

Thanks for your reply. Does this mean I have to add a String namefield in my controller and get value from VF page using set,get??  My VF page is below:

<apex:page standardController="Account" extensions="TestVFController"  sidebar="false" showHeader="false" >
    <apex:form >
    <apex:pageBlock title="Sales Contacts Quick Entry" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" alt="save" action="{!savereturn}" rerender="error" id="savebutton" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!conts}" var="a" id="table">
                <apex:column headerValue="First Name">
                    <apex:inputField value="{!a.FirstName}"/>
                </apex:column>               
                <apex:column headerValue="Last Name">
                    <apex:inputField value="{!a.LastName}"/>
                </apex:column>                
                <apex:column headerValue="Phone">
                    <apex:inputField value="{!a.Phone}"/>
                </apex:column>
                <apex:column headerValue="Email">
                    <apex:inputField value="{!a.Email}"/>
                </apex:column>
            </apex:pageBlockTable>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-weight:bold;">
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" />
&nbsp;|&nbsp;&nbsp;
            <apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />                
        </div>
    </apex:pageblockButtons>  
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

Could you please advice??

Maf_007Maf_007

Hi,

I have updated my code as below, I am still getting the same error:

 

Private Account ACID;
    public String LastName {get; set;}
    public String FirstName {get; set;}
    public List<Contact> Conts {get; set;}
    public Boolean refreshPage {get; set;}
    ApexPages.StandardController stdCtrl;

public TestVFController(ApexPages.StandardController myController) {
    	stdCtrl = myController;
    	refreshPage = false;
        This.ACID = (Account)stdCtrl.getRecord();
        Conts = new List<Contact>();
        Contact c = new Contact();
        c.accountid = ACID.id;
    	c.LastName = LastName;
    	c.FirstName = FirstName;
        Conts.add(c);
}

 

souvik9086souvik9086

Change it like this

 

public class TestVFController{
    Private Account ACID;
    public List<Contact> Conts {get; set;}
    public Boolean refreshPage {get; set;}
    ApexPages.StandardController stdCtrl;

public TestVFController(ApexPages.StandardController myController) {
    	stdCtrl = myController;
    	refreshPage = false;
        This.ACID = (Account)stdCtrl.getRecord();
        Conts = new List<Contact>();
        Contact c = new Contact();
        c.accountid = ACID.id;
		c.LastName = //give a name to your contact;
        Conts.add(c);
}
        
    public void addrow() {
        Conts.add(new Contact());}
            
    public void removerow(){
        Integer i = conts.size();
        Conts.remove(i-1);}
            
    public PageReference savereturn() {
        insert Conts;
        //PageReference result = stdctrl.view();
        PageReference result = new PageReference('/apex/test2');
        //PageReference result=ApexPages.currentpage();
        result.setRedirect(true);
        return result;
    }
        
    public PageReference OK(){
        refreshPage = true;
        PageReference pageRef = new PageReference('/apex/test1');
        //pageRef.setRedirect(true);
        return pageref;
    }
}

 Test Class

 

@isTest
public class TestForController{
    static testMethod void Testmycontroller() {
        List<Contact> c = new List<Contact>{};
        Account act = new Account(Name = 'Test acc');
        insert act;
        
        PageReference pageRef = Page.test1;
        Test.setCurrentPage(pageRef);
        
        // Instantiate the standard controller
        Apexpages.StandardController sc = new Apexpages.standardController(act);
        TestVFController mycon  = new TestVFController(sc);
        String nextPage = mycon.savereturn().getUrl();
        
        Contact con = new Contact();
        con.FirstName = 'Maf';
        con.LastName = 'Test';
        con.Email = 'maf@test.com';
        con.accountid = act.id;
        c.add(con);
        insert c;
        
        mycon.Conts.add(con);
        
        mycon.savereturn();
        mycon.addrow();
        mycon.removerow();
    }
}

 

Maf_007Maf_007

Hi,

 

Thanks for your reply. I have changed it  but still geting the same error?? please see code in previous post.

Dhaval PanchalDhaval Panchal

change below lines, no need to give last name in controller.

 

public TestVFController(ApexPages.StandardController myController) {
    	stdCtrl = myController;
    	refreshPage = false;
        This.ACID = (Account)stdCtrl.getRecord();
        Conts = new List<Contact>();
if(Test.isRunningtest()==false){ Contact c = new Contact();
c.LastName = LastName;
c.FirstName = FirstName; c.accountid = ACID.id; Conts.add(c);
} }

 

This was selected as the best answer
souvik9086souvik9086

 In your updated code you have not populated your string field lastname if it not coming from vf

 

Private Account ACID;
public String LastName {get; set;}
public String FirstName {get; set;}
public List<Contact> Conts {get; set;}
public Boolean refreshPage {get; set;}
ApexPages.StandardController stdCtrl;

public TestVFController(ApexPages.StandardController myController) {
stdCtrl = myController;
refreshPage = false;
This.ACID = (Account)stdCtrl.getRecord();
Conts = new List<Contact>();
Contact c = new Contact();
c.accountid = ACID.id;

LastName = //Give some value;
c.LastName = LastName;
c.FirstName = FirstName;
Conts.add(c);
}

Maf_007Maf_007

I needed that default contact just to add a default  row but did not want to give a value since I do not want to add unnecessary contact.

 

Thank you both of you guys. Your post open my eyes and put me in the right direction. Keep it up. Thanks again.