Skip to main content
Hi People,

I have written the folloing VF page usin custom controller and a test class. I am getting the following error. Plase help me solve this error

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

VF Page: showContacts

<apex:page controller="showContactsController" tabstyle="Account">

  <apex:pageBlock title="Recent Contacts">

  <apex:pageblockTable value="{!conList}" var="con">

  <apex:column >

  <apex:facet name="header">Salesforce Id</apex:facet>

  <apex:outputText value="{!con.id}"/>

  </apex:column>

  <apex:column >

  <apex:facet name="header">Name</apex:facet>

  <apex:outputText value="{!con.NAme}"/>

  </apex:column>

  </apex:pageblockTable>>

  </apex:pageBlock>

  

  <apex:form >

  <apex:pageBlock title="Redirect">

  <apex:commandButton value="Go To My Custom Page" action="{!doSubmit}"/>

  </apex:pageBlock>

  </apex:form>

  

  <apex:form >

  <apex:pageBlock title="Contact Form">

  FIRST NAME:<apex:inputText value="{!con.FirstName}"/><br/><br/>

  LAST NAME: <apex:inputText value="{!con.LastName}"/>

  </apex:pageBlock>

  <apex:commandButton value="Save" action="{!save}"/>

  </apex:form>

</apex:page>

Controller:  showContactsController

public with sharing class showContactsController {

    public List<Contact> conList{get;set;}

    public Contact con{get;set;}

    public showContactsController(){

        con = new  Contact();

        conList = [Select id,LastName,Name,Phone from Contact LIMIT 10];

    }

    

    public pagereference doSubmit(){

    pagereference pageRef = new pagereference('/apex/PaginationRecords');

    pageRef.setRedirect(true);

    return pageRef;

    }

    

    public pagereference save(){

        if(!String.isBlank(con.LastName))

            System.debug('LastName------------>'+con.LastName);

    insert con;

        System.debug('Contact id---------->'+con.id);

    return new pagereference('/'+con.id);

    }

}

Test Class for Controller: 

@isTest

public class showContactsController_Test {

    public static testMethod void controllerTest(){

        Account acc = new Account(Name='Test');

        insert acc;

        

        Contact con = new Contact(LastName='Test12',Phone='852149633',AccountId=acc.Id);

        insert con;

        

        Pagereference pageRef = Page.showContacts;

        pageRef.getParameters().put('LastName','Test123');

        pageRef.getParameters().put('id',con.id);

        Test.setCurrentPage(pageRef);

        

        Test.startTest();

        showContactsController controller = new showContactsController();

        PageReference pr1 = controller.doSubmit();

        Pagereference pr2 = controller.save();

        Test.stopTest();

        System.assertEquals(pr2.getUrl(),'/'+con.Id);

    }

}

 
3 answers
  1. Apr 21, 2019, 3:40 PM
    Hey Abhilash ,

    Either change in the save method like below 

    public pagereference save(){

            if(!String.isBlank(con.LastName)){

                System.debug('LastName------------>'+con.LastName);

                 insert con;

                System.debug('Contact id---------->'+con.id);

            }

        return new pagereference('/'+con.id);

    }

    Note : Use curly braces in if statement as good practice

    https://softwareengineering.stackexchange.com/questions/16528/single-statement-if-block-braces-or-no

    or if you don't want to change in your main class then try using the below

     

    @isTest

    public class showContactsController_Test {

    public static testMethod void controllerTest(){

    Account acc = new Account(Name='Test');

    insert acc;

    Contact con = new Contact(LastName='Test12',Phone='852149633',AccountId=acc.Id);

    insert con;

    Pagereference pageRef = Page.showContacts;

    pageRef.getParameters().put('LastName','Test123');

    pageRef.getParameters().put('id',con.id);

    Test.setCurrentPage(pageRef);

    Test.startTest();

    showContactsController controller = new showContactsController();

    PageReference pr1 = controller.doSubmit();

    controller.con.LastName = 'TestLName';

    Pagereference pr2 = controller.save();

    Test.stopTest();

    System.assertEquals(pr2.getUrl(),'/'+con.Id);

    }

    }

     
0/9000