+ Start a Discussion
CaptainObviousCaptainObvious 

Help with testMethod: Pre-populating fields on new Record

Some Background:

 

We have a custom object called GRE DI Code.

 

We create new GRE DI Codes from the Lead page through a custom link. Information on the GRE DI Code edit page is pre-populated with fields from the Lead and Account objects.

 

Previously, we accomplished this through an s-control.

 

We are attempting to migrate the functionality to Visualforce...

 

Visualforce Page:

 

<apex:page standardController="GRE_DI_Code__c" extensions="diCodeCreateExt" showHeader="true" sidebar="true" > <script type="text/javascript"> function populate() { //pre-populate vf page based on values from Lead & Account page: document.getElementById("{!$Component.myForm.pgB.pgBSec.gredcName}").value = 'GRE-' + '{!Lead.Di_Code__c}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.webSite}").value = '{!Lead.Website}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.fiName}").value = '{!Account.Name}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.greDC}").value = '{!Lead.Di_Code__c}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.app}").value = '{!Lead.Name}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.addr}").value = '{!Lead.Institution_Address__c}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.acc}").value = '{!Account.Name}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.city}").value = '{!Lead.Institution_City__c}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.dept}").value = '{!Lead.Department__c}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.state}").value = '{!Lead.Institution_State_Province__c}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.zip}").value = '{!Lead.Institution_Zip__c}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.phone}").value = '{!Lead.Institution_Phone__c}'; document.getElementById("{!$Component.myForm.pgB.pgBSec.email}").value = '{!Lead.Email}'; //pre-populate picklist var lvl_select = document.getElementById("{!$Component.myForm.pgB.pgBSec.lvl}"); var pVal1 = '{!Lead.Institution_Type__c}'; for( i=0; i < lvl_select.length; i++ ) { if( lvl_select.options[i].value == pVal1) { lvl_select.selectedIndex = i; break; } } //Another picklist... set the default to 'Active-To Be Published' var stat_select = document.getElementById("{!$Component.myForm.pgB.pgBSec.status}"); for( i=0; i < stat_select.length; i++ ) { if( stat_select.options[i].value == 'Active-To Be Published') { stat_select.selectedIndex = i; break; } } } window.onload=populate; </script> <style> .aField { width: 200px; } </style> <apex:sectionHeader title="GRE DI Code Edit" subtitle="New GRE DI Code" /> <apex:form id="myForm"> <apex:pageBlock title="GRE DI Code Edit" mode="edit" id="pgB"> <apex:pageMessages ></apex:pageMessages> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!save}"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Information" columns="2" id="pgBSec"> <apex:inputField id="gredcName" value="{!GRE_DI_Code__c.Name}" required="true"/> <apex:inputField id="webSite" value="{!GRE_DI_Code__c.Website__c}"/> <apex:InputField id="fiName" value="{!GRE_DI_Code__c.Full_Institution_Name__c}"/> <apex:inputField id="greDC" value="{!GRE_DI_Code__c.GRE_DI_Code__c}"/> <apex:inputField value="{!GRE_DI_Code__c.Legacy_Institution_Name__c}"/> <apex:inputField id="app" value="{!GRE_DI_Code__c.Application__c}"/> <apex:inputField id="addr" value="{!GRE_DI_Code__c.Address__c}" styleClass="aField"/> <apex:inputField id="acc" value="{!GRE_DI_Code__c.Account__c}"/> <apex:inputField id="city" value="{!GRE_DI_Code__c.City__c}"/> <apex:inputField id="dept" value="{!GRE_DI_Code__c.Department__c}"/> <apex:inputField id="state" value="{!GRE_DI_Code__c.State_Province__c}"/> <apex:inputField id="lvl" value="{!GRE_DI_Code__c.Level__c}"/> <apex:inputField id="zip" value="{!GRE_DI_Code__c.Zip_Postal_Code__c}"/> <apex:inputField id="phone" value="{!GRE_DI_Code__c.Phone__c}"/> <apex:inputField id="status" value="{!GRE_DI_Code__c.Status__c}"/> <apex:inputField id="email" value="{!GRE_DI_Code__c.Primary_Email__c}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

Controller Extension:

 

 

public class diCodeCreateExt { Lead lead; Account account; private String lid; public diCodeCreateExt(ApexPages.StandardController controller) { this.lid = ApexPages.currentPage().getParameters().get('lid'); } public Account getAccount() { account = [select id, name from Account where id = :Lead.Account__c]; return account; } public Lead getLead() { lead = [select id, name, account__c, di_code__c, website, institution_address__c, institution_city__c, institution_phone__c, institution_state_province__c, institution_type__c, department__c, institution_zip__c, email from Lead where id = :lid]; return lead; } static testMethod void testdiCodeCreate() { //Here's where we need help!! GRE_Di_Code__c gre = new GRE_Di_Code__c(Name='GRE-Test'); insert gre; System.assertEquals('GRE-Test', [select Name from GRE_Di_Code__c where id = :gre.id].Name); PageReference pageRef = Page.CreateDiCode; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('lid',gre.id); } }

 

We were able to successfully construct the Visualforce page and controller extension from the various examples in the force.com Cookbook, force.com Developer Guide, Apex Code Language Reference, and the Visualforce Developer's guide, but we haven't been able to write a successful test method.

 

Any tips/guidance would be greatfully appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
CaptainObviousCaptainObvious

Thanks for the help Ron!

 

Here's the test method with 100% coverage:

 

static testMethod void testdiCodeCreate() { GRE_Di_Code__c gre = new GRE_Di_Code__c(); //Create a reference to the VF page PageReference pageRef = Page.CreateDiCode; Test.setCurrentPageReference(pageRef); //Create an account and a lead: Account a = new Account(); a.name='TestAcct'; insert a; Lead l = new Lead(); l.LastName='Smith'; l.Company='TestCo.'; l.Account__c=a.id; insert l; //Set the 'lid' parameter ApexPages.currentPage().getParameters().put('lid',l.id); //Create an instance of the controller extension diCodeCreateExt di = new diCodeCreateExt(new ApexPages.StandardController(gre)); //Call the methods of the controller Lead LL = di.getLead(); Account AA = di.getAccount(); //Verify that test Lead and Account are as expected System.assertEquals('TestAcct', [select Name from Account where id = :LL.Account__c].Name); System.assertEquals('Smith', [select LastName from Lead where id = :LL.id].LastName); }

 

All Answers

Ron HessRon Hess

i think you are close, try something like this :

 

static testMethod void testdiCodeCreate() {

//Here's where we need help!!

GRE_Di_Code__c gre = new GRE_Di_Code__c(Name='GRE-Test');
insert gre;
System.assertEquals('GRE-Test', [select Name from GRE_Di_Code__c
where id = :gre.id].Name);

PageReference pageRef = Page.CreateDiCode;
Test.setCurrentPageReference(pageRef);
ApexPages.currentPage().getParameters().put('lid',gre.id);
diCodeCreateExt di = new diCodeCreateExt( new ApexPages.StandardController (gre ) );

// create a lead and account here

Account a = new Account(); insert a;

Lead l = new Lead( Account__c = a.id );

insert l;

 

// call methods of this controller

  Lead ll = di.getLead();

Account aa = di.getAccount();

/// add asserts here to verify ll and aa are as expected


}

 

 

this is not code that i've compiled, so you will have to tweak some stuff, but should get you started on testing

 

 

Message Edited by Ron Hess on 03-06-2009 04:33 PM
CaptainObviousCaptainObvious

Thanks for the help Ron!

 

Here's the test method with 100% coverage:

 

static testMethod void testdiCodeCreate() { GRE_Di_Code__c gre = new GRE_Di_Code__c(); //Create a reference to the VF page PageReference pageRef = Page.CreateDiCode; Test.setCurrentPageReference(pageRef); //Create an account and a lead: Account a = new Account(); a.name='TestAcct'; insert a; Lead l = new Lead(); l.LastName='Smith'; l.Company='TestCo.'; l.Account__c=a.id; insert l; //Set the 'lid' parameter ApexPages.currentPage().getParameters().put('lid',l.id); //Create an instance of the controller extension diCodeCreateExt di = new diCodeCreateExt(new ApexPages.StandardController(gre)); //Call the methods of the controller Lead LL = di.getLead(); Account AA = di.getAccount(); //Verify that test Lead and Account are as expected System.assertEquals('TestAcct', [select Name from Account where id = :LL.Account__c].Name); System.assertEquals('Smith', [select LastName from Lead where id = :LL.id].LastName); }

 

This was selected as the best answer