You need to sign in to do that
Don't have an account?
Need help making testmethod for VisualForce Controller
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.
I'm guessing you're wondering why this doesn't work:
You define cont as a list. ie. List<Contact>.
So if you say cont.Lastname - the system has no idea which Contact in the list you want to change.
You probably want to say something like: controller.cont[0].LastName (ie. the first element, but make sure it exists first or this won't work...)
That's the source of your problem.
I notice your test method doesn't actually test anything. Once you get it to work, add some asserts.
This is the standard Salesforce functionality that the "Last Name" is required field in contact. but in your code you are creating a contact record in constructor without providing it required "Last Name" field so at the run time it is giving error that "REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]: [LastName]" To correct this replace "cont.add(new Contact());" with "cont.add(new Contact(lastname='VKS'));" and in the test method you need not to write "controller = new multiContactInsert();" because when you create an object of a class its constructor is automatically called. You also need to create a contact record with last name in the test method before creating its object.
Try the following code. I think this should work.
I have just modified a little in your code. Please let me know, if you tried it, is working or not.
Regards,
Samarjit