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
angusgrantangusgrant 

Apex test class mimic visualforce page fields using controller Extension.

How can I get Apex to mimic passing the field data from the visualforce page to the controllerexstension save method. I have looked at 100's of examples yet still cant find a solution.:smileysad:

 

vf:

 

<apex:page standardController="contact" showHeader="false" extensions="appExtension">

<apex:composition template="StdOCEITemplate">
<apex:define name="pagebody">
<apex:form rendered="{!(SelectedEvent.Name != '')}" >
<apex:pageMessages />
<apex:pageBlock title="{!SelectedEvent.Name} - Application Form">
<apex:pageBlockSection title="Main Details" columns="2" >

<apex:inputField value="{!contact.Salutation}" id="title" required="true" />
<apex:outputText >&nbsp;</apex:outputText>
<apex:inputField value="{!contact.FirstName}" id="firstname" required="true" />
<apex:inputField value="{!contact.LastName}" id="lastname" />
<apex:inputField value="{!contact.Email}" id="email" required="true" />

<!-- more vf page here -->

  <apex:pageBlockButtons location="bottom" title="Submit">
<apex:commandButton action="{!save}"
value="Submit New Application"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:define>
</apex:composition>
</apex:page>

 

controller 

 


public class appExtension {

// Decare Vars from Querystring
Id accountrecordtypeid = ApexPages.currentPage().getParameters().get('AccountRecordTypeID');
Id contactrecordtypeid = ApexPages.currentPage().getParameters().get('ContactTypeID');
Id eventid = ApexPages.currentPage().getParameters().get('eventID');


public appExtension(ApexPages.StandardController controller) {
this.contact =
(Contact)controller.getRecord();
}

 /*More Controller code hear */

//This is the method that will save the details to the database
public PageReference save()
{
//set savepoint at start so as to stop account information being set if applicant has already applied for event.
Savepoint sp = Database.setSavepoint();
// Logic to make sure first letter of name/surname is in upper case
String FirstName = Contact.FirstName;
String LastName = Contact.LastName;
if (FirstName != null && LastName != null && FirstName != '' && LastName != ''){

//test method never gets in here

}

 test method:

 

public static testMethod void testappExtensionBusApp() {
ApexPages.standardController controller = new ApexPages.standardController(new Contact());
// Business applicant test where Business applicant selects an existing account.
ApexPages.currentPage().getParameters().put('AccountRecordTypeID',[Select ID from RecordType Where SobjectType = 'Account' and Name = 'Business Organisation' Limit 1].id);
ApexPages.currentPage().getParameters().put('ContactTypeID',[Select ID from RecordType Where SobjectType = 'Contact' and Name = 'Business Applicant' Limit 1].id);
ApexPages.currentPage().getParameters().put('eventID',[select id from SBS_Event__c Where Display_on_website__c = True and Event_Start_Date__c > Today Limit 1].id);

appExtension busextension = new appExtension(controller);

//select a random existing account to associate with.
busextension.picklistValue = [select name from Account limit 1].id;


// this is a insert because email is new in system
//decalre fields from application form
/* Contact contact = new Contact(
Firstname = 'testier',
Lastname = 'tester',
Email = 'test@test.com');
Attempt 1 does not get into If statment*/


/* busextension = new appExtension(controller);
busextension = Firstname('test.');
busextension = Lastname('testier');
busextension = Email('test@test.com');

Attempt 2 does not complile Method does not exist incorrect signiture */

String nextPage = busextension.save().getUrl();


busextension.save.getURL();
// Verify that the success page displays
System.assertEquals('/events/confirmsave', nextPage);

}
}

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
wesnoltewesnolte

Hey

 

1. Setup your contact entirely

2. Instantiate your StandardController

3. Use this in your extension

 

I.e.

 

Contact contact = new Contact(
Firstname = 'testier',
Lastname = 'tester',

Email = 'test@test.com');

 

ApexPages.standardController controller = new ApexPages.standardController(contact ); 

busextension = new appExtension(controller);

 

Cheers,

Wes 

 

 

All Answers

wesnoltewesnolte

Hey

 

1. Setup your contact entirely

2. Instantiate your StandardController

3. Use this in your extension

 

I.e.

 

Contact contact = new Contact(
Firstname = 'testier',
Lastname = 'tester',

Email = 'test@test.com');

 

ApexPages.standardController controller = new ApexPages.standardController(contact ); 

busextension = new appExtension(controller);

 

Cheers,

Wes 

 

 

This was selected as the best answer
hassabhassab

I am having a similar issue.  My Apex code is simple, just to be able to save a contact and redirect.  But the test gets stuck on the required field in the insert (the last name is required).  How do I pass a parameter to the insert in the test method?  Here's my code:

 

public class addContactAffiliation {Contact contact;

   public Contact getContact() {
      if(contact == null) contact = new Contact();
      string AccountID = System.currentPagereference().getParameters().get('AccountID');
      contact.System_Affil_OrgID__c = [select Name from Account where Id =: accountId].name;
     
      return contact;
   }

   public PageReference save() {

      // Add the account to the database.  
   
      insert contact;

      // Send the user to the detail page for the new account. 
   
      PageReference affilPage = new PageReference('/a0C/e?CF00N80000003PeoM=' + Contact.firstname + ' ' + Contact.lastname);
      affilPage.setRedirect(true);

      return affilPage;
   }

}

hassabhassab

Nevermind, found it.  Need to make contact public.

bella01bella01

Thanks for posting this code.  :)

it really helped in my development.