• bmercer
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

Hi,

 

I am new to APEX development and have been trying every idea I can find from these forums to make this visualforce controller pass the test threshold. 

 

The controller simply holds values for contact information entered in a visual force page, creating a new row for each new entry, and then inserting all entries when done. 

 

I have gotten it up to 72% in testing. But without being able to add a contact Last Name i get the error "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]"

 

On the other hand most attempts I have made to add a contact last name have not validated, producing a complie error, "Initial term of field expression must be a concrete SObject: LIST<Contact> at line 35 column 3."

 

Apex Code and TestMethod

 

 

public class multiContactInsert{

    public List<Contact> cont {get; set;}
    
    public multiContactInsert(){
        cont = new List<Contact>();
        cont.add(new Contact());
    }
    
    public void addrow(){
        cont.add(new Contact());
    }
    
    public PageReference save(){
        insert cont;
        PageReference home = new PageReference('/003?fcf=00B800000081Zoh');
        home.setRedirect(true);
        return home;
    }

///////////////////////
///TEST METHOD
//////////////////////


static testMethod void test()
{
  
  PageReference pageRef = Page.multiContactadd;
  Test.setCurrentPage(pageRef);

  multiContactInsert controller = new multiContactInsert();
  controller = new multiContactInsert();
  
  //controller.cont.LastName = 'Smith';
  // Not able to add lastname
  
  controller.addrow();

  controller.save();
}  



}

 

 

Here is the VisualForce page for reference.

 

 

<apex:page controller="multiContactInsert"
           sidebar="false">
    <apex:form >
        <apex:pageBlock >
        
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="error"/>
            </apex:pageBlockButtons>
        
            <apex:pageBlockTable value="{!cont}" var="c" id="table">
                <apex:facet name="footer">
                    <apex:commandLink value="Add Another Contact" action="{!addRow}" rerender="table,error"/>
                </apex:facet>
                <apex:column headerValue="First Name">
                    <apex:inputField value="{!c.FirstName}"/>
                </apex:column>
                <apex:column headerValue="Last Name">
                    <apex:inputField value="{!c.LastName}"/>
                </apex:column>
                <apex:column headerValue="Preferred Phone">
                    <apex:inputField value="{!c.npe01__PreferredPhone__c}"/>
                </apex:column>    
                <apex:column headerValue="Home Phone">
                    <apex:inputField value="{!c.HomePhone}"/>
                </apex:column>      
                <apex:column headerValue="Mobile Phone">
                    <apex:inputField value="{!c.MobilePhone}"/>
                </apex:column>     
                <apex:column headerValue="Preferred Email">
                    <apex:inputField value="{!c.npe01__Preferred_Email__c}"/>
                </apex:column>                       
                <apex:column headerValue="Personal Email">
                    <apex:inputField value="{!c.npe01__HomeEmail__c}"/>
                </apex:column>      
                <apex:column headerValue="Relationship to MMP">
                    <apex:inputField value="{!c.Relationship_to_MMP__c}"/>
                </apex:column>                                               
            </apex:pageBlockTable>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Thanks, any help is greately appreciated.