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
notsosmartnotsosmart 

Test Method Wrapper Class reference

I have fully functional code using wrapper classes.  My trouble is referencing the wrapper class fields for population in the test method.

 

All reference attempts (modeling the controller extension) references to the wrapper elements result in syntax errors.

The most common is 'variable does not exist.

 

It could be the method call because I also could not pass syntax on attendeeLineCount++

 

excepts are:  The problem code is commented out and the error copied to begining comment line marked with ***

 

Any ideas?  Thanks.

 

Wrapper class:

 

/**
* Purpose : This class is a supporting Data Transfer Objects for the Trip Report custom UI.
*/
public class AttendeeLineItem {

public Integer lineNumber{ get; set; }
public Boolean isDeleted{ get; set; }
public tripContactAssociation__c attendeeitem { get; set;}

public attendeeLineItem(){
attendeeitem = new tripContactAssociation__c();
lineNumber = 0;
isDeleted = false;
}

public AttendeeLineItem(Integer pLineNumber, tripContactAssociation__c pAttendeeItem){
attendeeitem = pAttendeeItem;
lineNumber = pLineNumber;
isDeleted = false;
}
}

 

-----

 

Reference int the controller extension:  

 


public List<AttendeeLineItem> getAttendeeItemListNotDeleted(){
//List for display on the view.
List<AttendeeLineItem> listToDisplay = new List<AttendeeLineItem>();
for (AttendeeLineItem item : attendeeLineItemList){
if (item.isDeleted != true){
listToDisplay.add(item);
}
}

return listToDisplay;
}

--

public Integer selectLineNumber{ 
get{
if (selectLineNumber == null) {
selectLineNumber = 0;

return selectLineNumber;
}
set; 
}

 

--

//List of wrapper objects.
private List<AttendeeLineItem> attendeeLineItemList{
get{
if (attendeeLineItemList == null){
attendeeLineItemList = new List<AttendeeLineItem>();

//Only loaded on isUpdate.
if (isUpdate){

for(TripContactAssociation__c attendee : attendeeList ){
attendeeLineCount ++;
AttendeeLineItem lineItem = new AttendeeLineItem( attendeeLineCount , attendee);
attendeeLineItemList.add(lineItem);
}
}
}
return attendeeLineItemList;
}
set;
}

//Renumber the lines in the list.
private void renumberAttendeeLineItemList(){
attendeeLineCount = 0;
for ( AttendeeLineItem item : attendeeLineItemList ){
item.lineNumber = 0;
if (item.isDeleted == false){
attendeeLineCount++;
item.lineNumber = attendeeLineCount;
}
}
}

--

//Action method to add a new attendee item in the view.
public PageReference addAttendee(){

reportItemsMessage = null;
ApexPages.getMessages().clear();

if ( attendeeLineCount < MAX_ATTENDEE_ITEM_NUM ){
//Add a new empty row to the list.
attendeeLineCount++;
attendeeLineItemList.add(new AttendeeLineItem( attendeeLineCount, new TripContactAssociation__c( Attendee__c = reportId ) ));
reportItemsMessage = null;
} else {
//Display a message.
reportItemsMessage = 'No more than ' + String.valueOf(MAX_ATTENDEE_ITEM_NUM) + ' attendees are permitted.';
}

return null;
}

 

--

//Action method to remove an attendee item from display by issuing a soft delete.
public PageReference removeAttendee(){

reportItemsMessage = null;
ApexPages.getMessages().clear();

if (selectLineNumber != Null ){

//Soft delete the line and renumber.
for ( AttendeeLineItem item : attendeeLineItemList ){
if (item.lineNumber == selectLineNumber){
item.isDeleted = true;
break;
}
}
renumberAttendeeLineItemList();
}

reportItemsMessage = null;
return null;
}

 

--

   In the save method -

 

List<TripContactAssociation__c> attendeeItemsToDelete = new List<TripContactAssociation__c>();
List<TripContactAssociation__c> attendeeItemsToUpsert = new List<TripContactAssociation__c>();

//Loop thru the lines and capture those needing deletion or upsert.
//There may be some to ignore - newly added and subsequently removed,
for ( AttendeeLineItem item : attendeeLineItemList ){
if (item.isDeleted == true && item.attendeeitem.id != null){
attendeeItemsToDelete.add(item.attendeeitem);
}
if (item.isDeleted == false){
attendeeItemsToUpsert.add(item.attendeeitem);
}
}

 

--

 

//Save the items.
//Populate new attendee items with report id.
for ( TripContactAssociation__c item : attendeeItemsToUpsert ){
if (item.Attendee__c == null){
item.Attendee__c = tripReport.id;
}
}

tripReport = TripRptManager.getTripReportId(tripReport.unique_Name__c);

//Items upsert should follow to insure report ids.
if (attendeeItemsToUpsert.size() > 0 ){

//Save the new or changed items.
try{
upsert attendeeItemsToUpsert;
} catch (system.Exception e) {
//Abandon.
Database.rollback(sp);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Error saving Attendee items.'));
return Page.TripRptStep2;
}
}

 

NOW The test method:

 

@isTest private class TestTripRptExt1 {
Trip_Report__c tripReport = new Trip_Report__c();
ApexPages.StandardController thecontroller = new ApexPages.StandardController(tripReport);

TripRptExt controller = new TripRptExt(thecontroller);
static testMethod void testTripRptExt() {

PageReference pageRef = Page.TripRptStep1;
Test.setCurrentPage(pageRef);

//set the field values of details
//all the field values those are entered in VF page has to be set here
Trip_Report__c tripReport = new Trip_Report__c();

//Instantiate and construct the controller class.


ApexPages.StandardController thecontroller = new ApexPages.StandardController(tripReport);

TripRptExt controller = new TripRptExt(thecontroller);

// controller.setUnique_Name & basic fields(
tripReport.Unique_Name__c = 'Test Method Name';
tripReport.Date_of_Meeting__c = date.parse('07/13/2012');
tripReport.Meeting_Notes__c = 'Test Method Note';

//The .getURL will return the page url method returns.

String nextPage = controller.Step2().getUrl();

PageReference pageRefN2 = Page.TripRptStep2;
Test.setCurrentPage(pageRefN2);

// test Cancel functions
String cancel = controller.cancel().getUrl();
String nocancel = controller.nocancel().getUrl();

Test.setCurrentPage(pageRefN2);

// PageReference List<AttendeeLineItem>;

PageReference addAttendee;

PageReference removeAttendee;

PageReference pageRefN3 = Page.TripRptStep3;
Test.setCurrentPage(pageRefN3);

PageReference addOpportunity;

PageReference removeOpportunity;

PageReference pageRefN4 = Page.TripRptStep4;
Test.setCurrentPage(pageRefN4);
String TripRptStep5 = controller.savereport().getUrl();

PageReference pageRefN6 = Page.TripRptStep6;
Test.setCurrentPage(pageRefN6);

PageReference pagePdfN = Page.TripRptPdf;
Test.setCurrentPage(pagePdfN);

String done = controller.done().getUrl();

// save should have errored on no attendees. Add 1 & save

PageReference pageRefN2b = Page.TripRptStep2;
Test.setCurrentPage(pageRefN2b);
Test.setCurrentPage(pageRefN2b);


TripRptManager.getAttendeesForReport('');


// Public List<AttendeLineItem>getAttedeeItemListNotDeleted;

PageReference addAttendee2;


// attendeeLineCount++;
// done in add function attendeeLineItemList.add(new AttendeeLineItem( 1, new TripContactAssociation__c( Attendee__c = '', Contact__c = 'Craig' ) ));

/* *** ERROR IS Variable does not exist: attendeeLineItemList at line 86 column 31

for(AttendeeLineItem item: attendeeLineItemList) {
item.Contact__c = 'Craig';
}
*/
PageReference pageRefN5 = Page.TripRptStep5;
Test.setCurrentPage(pageRefN5);
String TripRptStep5b = controller.savereport().getUrl();



//Check that the save() method returns the proper URL
// System.assertEquals('/apex/failure?error=noParam', nextPage);

// Next Test update functions
}
}