• Bob Bailey
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies

Trying to find a way around my previous testing problem I added this method to the test class:

static testMethod void testContributionSearchControllerDirect () {
      //instantiate a page     
     PageReference pg = Page.Contributions_Search_v9;
     // instantiate the controller
        ContributionsSearchController_v9 controller = new ContributionsSearchController_v9();
    // Plug in variable values
       controller.setcontributorFname('a');
       controller.setcontributorLname('z');
       controller.setMOC_Lname('g');
       controller.setsoqlLimitPage('limit 20');

 

The controller has this code between the class defn and the constructor:

   Public string contributorFname{
       get {return contributorFname; }
       set;
       }
   Public string contributorLname{
       get {return contributorLname; }
       set;
       }
   Public string MOC_Lname{
       get {return MOC_Lname; }
       set;
       }
   Public string soqlLimitPage{
       get {return soqlLimitPage; }
       set;
       } 


I get an error when I try to setContributorFname:
Error: Compile Error: Method does not exist or incorrect signature: [ContributionsSearchController_v9].setcontributorFname(String) at line 12 column 8. This is the line:

       controller.setcontributorFname('a');

 

I get the same error with the other lines if I comment out the first offender.

 

I'm baffled by this, I think I'm using the setter properly but clearly something is wrong. Thanks for any help.

 

....Bob

I changed some code to act based on a button click rather than through JavaScript. The VF and Apex Class work very well. However, when I went to test it...

 

First, let me admit that I am flailing in the dark when it comes to testing. Someone else wrote the test code and it worked. I kind of understand it but not that well.

 

Here's the test class:

@isTest
private class TEST_ContributionsSearch_v9 {
    
     static testMethod void testContributionSearchControllerEmpty () {
      //instantiate a page     
     PageReference pg = Page.Contributions_Search_v9;
        Test.setCurrentPage(pg);
        pg.getParameters().put('contributorFname', '');
        pg.getParameters().put('contributorLname', '');
        pg.getParameters().put('MOC_Lname', '');
        pg.getParameters().put('soqlLimitPage', '');
     // instantiate the controller
        ContributionsSearchController_v9 controller=new ContributionsSearchController_v9();
        System.assert(controller.runQuery() == null);
        System.assert(controller.runPicklists() == null);
      }
      
     static testMethod void testContributionSearchController () {
      //instantiate a page     
     PageReference pg = Page.Contributions_Search_v9;
        Test.setCurrentPage(pg);
        pg.getParameters().put('contributorFname', 'a');
        pg.getParameters().put('contributorLname', 'z');
        pg.getParameters().put('MOC_Lname', 'g');
        pg.getParameters().put('soqlLimitPage', 'limit 20');
     // instantiate the controller
        ContributionsSearchController_v9 controller=new ContributionsSearchController_v9();
        System.assert(controller.runSearch() == null);
      }    
 }

 Before the change contributorFname etc. were all passed to the controller from the VF by means of JS and  <apex:actionFunction...

 

Now, contributorFname etc.are public strings in the controller with getters and setters.

 

I get the message: "System.NullPointerException: Attempt to de-reference a null object"

Stack Trace: "Class.ContributionsSearchController_v9.runSearch: line 94, column 1 Class.TEST_ContributionsSearch_v9.testContributionSearchController: line 30, column 1"

Line 30 is the second assert: "System.assert(controller.runSearch() == null);"

Line 94 references the variable contributorFname in this statement:

    if (!contributorFname.equals(''))
      soql_C += ' and Contributor__r.firstname  LIKE \''+String.escapeSingleQuotes(contributorFname )+'%\'';

 

The first section of the test runs fine. I suspect that because the button doesn't get "clicked" the variables aren't set.

 

I'm happy to read doc on this but can't seem to find it. Links are welcome.

 

The controller and VF page follow my thanks.

 

THANKS... Bob

 

The Controller:

/*  v7 fix repopulation search criteria issues */
/*  v8 Clean out the remains of the javaScript call */
/*  v9 Add other parms, create production candidate */
public with sharing class ContributionsSearchController_v9{
  
// the soql without the order and limit
  private String soql_C {get;set;}  
// the collection of contributions to display
  public List<Contributions__c> contributions {get;set;} 
// This is the base soql for building the query
  public String soqlBase_C {
    get{ return soqlBase_C ; }
    set;
   } 
// format the runReport URL for display on the visualforce page
  public String runReport{
    get{ return 'https://cs10.salesforce.com/' ; }
    set;
   }
// Create a query variable for debugging
  public String theQuery{
   get {return theQuery; }
   set;
   }
// Create a variable for record returned count
  Public Integer numberContributions{
   get ;
   set;
   }
// Create a STRING variable for record returned count
  Public string numberContributionsStr{
    get{ return numberContributionsStr; }
   set;
   }
// a set of variables to persist the search parameters   
   Public string contributorFname{ 
       get {return contributorFname; }
       set ;
       }
   Public string contributorLname{ 
       get {return contributorLname; }
       set; 
       }
   Public string MOC_Lname{ 
       get {return MOC_Lname; }
       set; 
       }
   Public string soqlLimitPage{ 
       get {return soqlLimitPage; }
       set; 
       }
                                                     // END OF VARIABLE Declarations
   
  // init the controller and display some sample data when the page loads CONTRUCTOR
  public ContributionsSearchController_v9() {
    system.debug('v9.0');
    system.debug('NO report button');
    soqlBase_C = 'select ID, name, Contribution_Amount__c, Contribution_Date__c,  Contribution_ID__c, Contribution_Type__c, Contributor__c, Contributor__r.firstname, Contributor__r.lastname, Contributor_City__c, Contributor_FEC_ID__c, Contributor_State__c, Contributor_Zip__c, Election_Cycle__c, MOC__c, MOC__r.firstname, MOC__r.lastname from Contributions__c where ID !=null ';
    system.debug(soqlBase_C);
    soql_C = soqlBase_C;
    runQuery();
  }                                                  // end CONTRUCTOR
  // runs the actual query
  public void runQuery() {     
    try {                                            // Get Contribution Records
      if(soqlLimitPage == null) {
        soqlLimitPage = 'limit 20';
        system.debug('TRY set null soqlLimitPage = ' + soqlLimitPage );
        }
      theQuery = soql_C + ' ' + soqlLimitPage;
      system.debug(' theQuery = ' + theQuery);  
      contributions = Database.query(theQuery);
      system.debug('Contributions list returned = ' + contributions);  
      numberContributions = contributions.size();
      numberContributionsStr = String.valueOf(numberContributions );  
    }               // end try
    catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, string.valueof(e))); 
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, soqlLimitPage));
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, theQuery));
    }              // end catch
  }                // end runQuery
 
  // runs the search with parameters passed via variables
  public PageReference runSearch() {
                                         // Display search variables
    system.debug('contributorFname = ' + contributorFname );
    system.debug('contributorLname = ' + contributorLname );
    system.debug('MOC_Lname = ' + MOC_LName);
    system.debug('soqlLimitPage= ' + soqlLimitPage);
                                        // Build the rest of the soql_ by tacking on and operator parameter  
    soql_C = soqlBase_C;                                          
    if (!contributorFname.equals(''))
      soql_C += ' and Contributor__r.firstname  LIKE \''+String.escapeSingleQuotes(contributorFname )+'%\'';
    if (!contributorLname.equals(''))
      soql_C += ' and Contributor__r.lastname LIKE \''+String.escapeSingleQuotes(contributorLname )+'%\'';
    if (!MOC_Lname.equals(''))
      soql_C += ' and MOC__r.lastname LIKE \''+String.escapeSingleQuotes(MOC_Lname )+'%\'';
    //  MOC__r.firstname, 
                                        // now, go run the query again and exit
    runQuery();
    return null;
  }                // end runSearch
  
  
  // tests the variables, only function is testing
  Public void runPicklists() {
      List<String> junkList;
      String junkString;      
      junkString = soqlLimitPage;
  }              // end runPicklists   
}                // end class

 The VisualForce Page:

<apex:page controller="ContributionsSearchController_v9" sidebar="false">
 
  <apex:form >
  <apex:pageMessages id="errors" />
 
  <apex:pageBlock title="Contributions Search v9" mode="edit">
 
  <table width="100%" border="0">
  <tr>  
    <td width="200" valign="top"> 
      <apex:pageBlock title="Search Criteria" mode="edit" id="criteria">
      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">Contributor First Name<br/>
        <apex:inputText id="contributorFname" value="{!contributorFname}"/>
        </td>
      </tr>   
      <tr>
        <td style="font-weight:bold;">Contributor Last Name<br/>
        <apex:inputText id="contributorLname" value="{!contributorLname}"/>
        </td>
      </tr>   
      <tr>
        <td style="font-weight:bold;">MOC Last Name<br/>
        <apex:inputText id="MOC_Lname" value="{!MOC_Lname}"/>
        </td>
      </tr>        
      <tr>
        <td style="font-weight: bold;">Record Limit<br/>
           <apex:selectlist value="{!soqlLimitPage}" multiselect="false" size="1">
              <apex:selectOption itemValue="limit 20" itemLabel="20 Records"/>
              <apex:selectOption itemValue="limit 40" itemLabel="40 Records"/>
              <apex:selectOption itemValue="limit 80" itemLabel="80 Records"/>
           </apex:selectlist>           
      </td>
      </tr>
      <tr>
        <td style="font-weight: bold;"><br/>
          <apex:commandButton action="{!runSearch}" value="Run Search" rerender="results,debug" id="theSearch"/>         
      </td>
      </tr>
    </table>      
    </apex:pageBlock>
 
    </td>
    <td valign="top">
 
    <apex:pageBlock mode="edit" id="results">
        <apex:pageBlockTable value="{!contributions}" var="contribution">          
            <apex:column headerValue="Name" >
                <apex:outputField value="{!contribution.name}"/>
            </apex:column>             
            <apex:column headerValue="Amount" >
                <apex:outputField value="{!contribution.Contribution_Amount__c}"/>
            </apex:column>           
            <apex:column headerValue="Date" >
                <apex:outputField value="{!contribution.Contribution_Date__c}"/>
            </apex:column>           
            <apex:column headerValue="Contributor First Name" >
                <apex:outputField value="{!contribution.Contributor__r.firstname}"/>
            </apex:column>           
            <apex:column headerValue="Contributor Last Name" >
                <apex:outputField value="{!contribution.Contributor__r.lastname}"/>
            </apex:column>           
            <apex:column headerValue="City" >
                <apex:outputField value="{!contribution.Contributor_City__c}"/>
            </apex:column>           
            <apex:column headerValue="State" >
                <apex:outputField value="{!contribution.Contributor_State__c}"/>
            </apex:column>           
            <apex:column headerValue="Zip" >
                <apex:outputField value="{!contribution.Contributor_Zip__c}"/>
            </apex:column>           
            <apex:column headerValue="Cycle" >
                <apex:outputField value="{!contribution.Election_Cycle__c}"/>
            </apex:column>           
            <apex:column headerValue="MOC First Name" >
                <apex:outputField value="{!contribution.MOC__r.firstname}"/>
            </apex:column> 
            <apex:column headerValue="MOC Last Name" >
                <apex:outputField value="{!contribution.MOC__r.lastname}"/>
            </apex:column>                       
          </apex:pageBlockTable>
          
          <br/>
          <br/>
           <apex:outputText style="font-weight: bold;" value=" Note:  {!numberContributionsStr} Contribution Records match your criteria. " /> 
          <br/>
          <br/>
           <apex:outputText value=" To see more records, change the Record Limit criteria above IF {!numberContributionsStr} was the limit.  " /> 
          <br/>
          <br/>
           <apex:outputText value="Contributions Search v9" /> 
          <br/>
          <br/>                  
    </apex:pageBlock>
 
    </td>
  </tr>
  </table>
 
  <apex:pageBlock title="Debug - SOQL - theQuery" id="debug">
      <apex:outputText value="{!theQuery}" /> 
      <br/>          
      <apex:outputText value="Records Returned: {!numberContributionsStr}" /> 
      <br/>          
  </apex:pageBlock>     
 </apex:pageBlock> 
 </apex:form>
 
</apex:page>

 

 

 

Hi,

 

Hats off to Jeff Douglas for this wonderful piece of work. I've use it successfully more or less as is.

http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/

 

I now have a search that really needs to be button driven, partly from a combo of fast typing and huge amount of data.

 

It seemed to me that "all I had to do" was:

  • Create a button with onclick="doSearch();"
  • Remove onkeyup="doSearch();" from the search criteria fields
  • fill the fields and click the button

Clearly not. I have gotten mixed results. The search doesn't happen. Sometimes it appears that the page is re-entered after the search is done and the page is rendered. That is,

  • fill the fields and click the button
  • Page is rerendered
  • Page is rerendered again

I can see this in the logs. The soql string has the search parameters: "and Contributor__r.firstname LIKE 'ag%"

 

Moreover, if I have a "mixed" page with the button and the onkeyups the search works.

 

Any experience with changing Jeff's code to button click? Any ideas what I am doing wrong?

 

Thanks in advance... Bob

 

I'm sure you'll all be pleased to know that my search is working and I won't be bugging you about that anymore. However, I still have one small problem.

 

I have a button in the search that links to a report:

          <apex:commandButton action="{!$Site.CurrentSiteUrl}/00OJ0000000O9cH?pv0={!contributorFname}&pv1={!contributorLname}&pv02=''" value="Run Report" rerender="results,debug" id="theReport"/>         

 and it works. Sort of, it does link and it does report. But only if contributorFname AND contributorLname have values that make sense to a "starts with" report filter. Is there a way to pass values such that the filter is ignored?

 

Assuming the answer to that is no... I'm doing this to facilitate exporting the resulting search table. Is there a way to do that in Visualforce? I apologize if this is a basic duh question.

 

Thanks... Bob

 

 

 

  • July 23, 2012
  • Like
  • 0

Trying to find a way around my previous testing problem I added this method to the test class:

static testMethod void testContributionSearchControllerDirect () {
      //instantiate a page     
     PageReference pg = Page.Contributions_Search_v9;
     // instantiate the controller
        ContributionsSearchController_v9 controller = new ContributionsSearchController_v9();
    // Plug in variable values
       controller.setcontributorFname('a');
       controller.setcontributorLname('z');
       controller.setMOC_Lname('g');
       controller.setsoqlLimitPage('limit 20');

 

The controller has this code between the class defn and the constructor:

   Public string contributorFname{
       get {return contributorFname; }
       set;
       }
   Public string contributorLname{
       get {return contributorLname; }
       set;
       }
   Public string MOC_Lname{
       get {return MOC_Lname; }
       set;
       }
   Public string soqlLimitPage{
       get {return soqlLimitPage; }
       set;
       } 


I get an error when I try to setContributorFname:
Error: Compile Error: Method does not exist or incorrect signature: [ContributionsSearchController_v9].setcontributorFname(String) at line 12 column 8. This is the line:

       controller.setcontributorFname('a');

 

I get the same error with the other lines if I comment out the first offender.

 

I'm baffled by this, I think I'm using the setter properly but clearly something is wrong. Thanks for any help.

 

....Bob

Hi,

 

I am little confused with getter and setter methods for standard fileds on standard objects.

 

for example: On Account Object, for field Type, how do i use getter and setter methods.

 

VF Code:

 

<apex:page standardcontroller="Account" extensions="customSearch">
    <apex:form >
        <apex:pageBlock title="Custom Search" mode="Edit">
            <apex:pageblockSection title="Select Fields" columns="1" >
                <apex:inputField value="{!Account.Type}"/>
                <apex:inputField value="{!Account.Rating}"/><br/>                                                              
            </apex:pageblockSection>  
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Search" action="{!doSearch}" reRender="SearchResults" />                
            </apex:pageBlockButtons>                                                        
        </apex:pageBlock>

 

As you can see,input field is {!Account.Type} gives us the picklist and values.

From here how do i pass the value back to controller.

 

My Controller:

 

public class customSearch {

    public customSearch(ApexPages.StandardController controller) {

    }
   /* Public Account acc=new Account();
    public Account getacc(){
        return acc;
    }*/
   String Account_Type;
   public String getAccount_Type(){
       return Account_Type;
   }
   public string setAccount_Type(string Account_Type){
       Account_Type=Account_Type;
       return Account_Type;
      }
    
    public List<Account> results=new List<Account>();
    
    public void doSearch(){
        System.debug('account Type == '+Account_Type);
        results=[Select Name, Type from Account where Type=:Account_Type ];
       // return null;
        }
        
     public List<Account> getResults(){
         return results;
        }
}

As you can see, i am trying to filter records based on the Type value.

But, when i runa debug log, the "Account_Type " is NULL.

 

I am a little confused on how to set and get standard field values.

Can any one help me point in right direction.

 

Thanks for your time!

 

Sales4ce.