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
EMHDevEMHDev 

Problem with setting VF inputfield from test method

I am struggling to set inputfield values from my unit tests.  I have read that I need to do it using

ApexPages.currentPage().getParameters().put(key,value)

 

But I can't work out how to associate the key with the inputfield.  I'm sure I'm missing something basic but have been struggling for days and haven't managed to get the values set.  Do I need to write setters for the inputField, and if so, how do I reference them?

 

Here is the VF code - very simple page:

 

<apex:page standardController="Call_Meeting_Report__c" extensions="ReportSearchController" showHeader="true" sidebar="true" action="{!loadDefaults}">
<apex:form >
<apex:pageBlock title="Call/Meeting Report" mode="edit" id="thePageBlock">
<apex:pageMessages />
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1">
<apex:inputField value="{!Call_Meeting_Report__c.Account__c}" required="true">
<apex:actionSupport event="onChange" action="{!getDetails}" rerender="opps, conts" />
<apex:actionSupport event="onclick" action="{!getDetails}" rerender="opps, conts"/>
</apex:inputField>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Opportunity "/>
<apex:selectList value="{!Call_Meeting_Report__c.Opportunity__c}" id="opps" size="1">
<apex:selectOptions value="{!Opps}"/>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Contact "/>
<apex:selectList value="{!Call_Meeting_Report__c.Primary_Contact__c}" id="conts" size="1">
<apex:selectOptions value="{!Conts}"/>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:inputField required="true" value="{!Call_Meeting_Report__c.Date__c}" />
<apex:inputField value="{!Call_Meeting_Report__c.Type__c}" required="true"/>
<apex:inputField value="{!Call_Meeting_Report__c.Purpose__c}" required="true"/>
<apex:inputField value="{!Call_Meeting_Report__c.Other_people_present__c}" style="width: 100%;" required="true"/>
<apex:inputField value="{!Call_Meeting_Report__c.Call_Meeting_Objectives__c}" style="width: 100%;" required="true"/>
<apex:inputField value="{!Call_Meeting_Report__c.Discussion__c}" style="width: 100%;" required="true"/>
<apex:inputField value="{!Call_Meeting_Report__c.Next_actions__c}" style="width: 100%;" required="true"/>
<apex:pageBlockSectionItem />
</apex:pageBlockSection>
</apex:pageBlock>

</apex:form>
</apex:page>

 The one I'm initially trying to get work is the Account__c field, but I'll need to do it with Opportunity__c and Primary_Contact__c as well to get the required code coverage.

 

Here is the test method that is (not) setting the value - although the accid parameter is being set, it is not bound to the account__c field:

 

public static testmethod void testloadDefaultsfromAcc() {
/* This is a basic test which simulates the primary positive case for the
load defaults method in the ReportSearchController class. */
Account acc = new Account();
Opportunity o = new Opportunity();
Contact c = new Contact();

setupData(acc, o, c); // inserts an account, contact, opportunity and OpportunityContactRole and relates them all

PageReference pref = Page.NewCallMeetingReport;
pref.getParameters().put('aid', acc.id);
Test.setCurrentPage(pref);

system.debug('@@@ aid '+pref.getParameters().get('aid'));
// Instantiate a new controller
ApexPages.StandardController con = new ApexPages.StandardController(new Call_Meeting_Report__c());

// Switch to runtime context
Test.startTest();
//Construct the ReportSearchController class
ReportSearchController rep = new ReportSearchController(con);

rep.loadDefaults();
Test.stopTest();

}

 

 

 

 If I do a System.AssertEquals on the account id, it always fails as the account is always null.  Please can someone help me regarding how to "put" my test data into the relevant fields?

 

The relevant bit of my controller is here - rather complex because the page is related to 3 objects and I need to detect where it is being called from.

 

 

//add an instance variable for the standard controller
private ApexPages.StandardController controller {get; set;}
// the actual call/meeting report
private Call_Meeting_Report__c cmr {get; set;}
private Id a, newa, OppThere, ContThere;

public List <selectOption> Opps {get; set;}
public List <selectOption> Conts {get; set;}
private String accid;


// Initialise the controller
public ReportSearchController(ApexPages.StandardController controller) {

//initialise the standard controller
this.controller = controller;
cmr = (Call_Meeting_Report__c)controller.getRecord();
this.accid = ApexPages.currentPage().getParameters().get('accid');

}

 

public void loadDefaults() {
Opps = new List<selectOption>();
Conts = new List<selectOption>();
String OppName, ContName;
Boolean fromOpp, fromCont, fromAcc;

a = cmr.Account__c;
fromAcc = (a!=null);
OppThere = cmr.Opportunity__c;
fromOpp = (OppThere != null);
ContThere = cmr.Primary_Contact__c;
fromCont = (ContThere != null);
system.debug('### Account is '+a+' Opp is '+OppThere+' Contact is '+ContThere);
// use some dynamic soql to find the related opportunities / contacts by account name

try {
if (fromAcc){ // populate Opportunity and Contact pulldowns
popOpplist();
popContlist();
} else {
if (fromOpp){
/* We need to 1. populate the Opportunity pulldown with the opportunity name
2. get the Account from the Opportunity and populate the Account field
*/
List <Opportunity> opplist = [Select Id, Account.Name, AccountId, Name from Opportunity where Id =:OppThere];
if (opplist.size() > 0) {
oppName = opplist[0].Name;
cmr.Account__c = opplist[0].AccountId;
a = cmr.Account__c;
system.debug('### setting account to '+cmr.Account__c);
} else {
system.debug('### No opps for '+OppThere+' - impossible?');
oppName = '---None---';
}
system.debug('### Opp already there, adding '+oppName);
Opps.add(new selectOption(OppThere, oppName));
popContlist(OppThere);
} else {
if (fromCont){
/* We need to 1. populate the Contacts pulldown with the Contact name
2. get the Account from the Contact and populate the Account field
*/
List <Contact> contlist = [Select Id, Account.Name, AccountId, Name from Contact where Id =:ContThere];
if (contlist.size() > 0) {
contName = contlist[0].Name;
cmr.Account__c = contlist[0].AccountId;
a = cmr.Account__c;
system.debug('### setting account to '+cmr.Account__c);
} else {
contName = '---None---';
system.debug('### No contacts for '+ContThere+' - impossible?');
}
system.debug('### Contact already there, adding '+ContThere);
Conts.add(new selectOption(contThere, contName));
popOpplist();
}
}
}
}
catch (Exception e) {
System.debug('### error '+e);
ApexPages.addMessages(e);
}


}

 

 

 Would be really grateful for any help.

 

Best Answer chosen by Admin (Salesforce Developers) 
Anand@SAASAnand@SAAS

When you are constructing the "StandardController", create the Call_Meeting_Report with the Account__c set. here's how that code may look:

 

Call_Meeting_Report__c report = new Call_Meeting_Report__c(Account__c=acc.Id,Opportunity__c=o.Id,Contact__c=c.Id); ApexPages.StandardController con = new ApexPages.StandardController(report);

 Passing the values via the URL does'nt set the values on an <apex:inputField>. You need to set the values on the underlying controller variables. The only exception to this is the "id" parameter when using the standard controller.

 

All Answers

Anand@SAASAnand@SAAS

When you are constructing the "StandardController", create the Call_Meeting_Report with the Account__c set. here's how that code may look:

 

Call_Meeting_Report__c report = new Call_Meeting_Report__c(Account__c=acc.Id,Opportunity__c=o.Id,Contact__c=c.Id); ApexPages.StandardController con = new ApexPages.StandardController(report);

 Passing the values via the URL does'nt set the values on an <apex:inputField>. You need to set the values on the underlying controller variables. The only exception to this is the "id" parameter when using the standard controller.

 

This was selected as the best answer
EMHDevEMHDev

Thanks so much, this was the missing piece in the jigsaw - never saw it in any documentation and am not a Java or C# programmer so am not familiar with this approach.   It works beautifully and I have 86% coverage now.

 

Thanks again.

Message Edited by m62Dev on 02-15-2010 06:30 PM