[SalesForce] Search and ReRender in Visualforce page

I am trying to add a search box to a Visual force page that renders a table. The page worked fine before adding the search. And now it works fine so long as I don't enter a search term.

When I leave the search box blank and press the "Search" button I get:

Return type of an Apex action method must be a PageReference. Found: visualforce.el.VisualforceArrayList

If I enter a term and press "search" I get:

System.QueryException: Didn't understand relationship 'res' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Error is in expression '{!getReservations}' in component in page reservation_list: Class.ReservationListController.getReservations: line 20, column 1
Class.ReservationListController.getReservations: line 20, column 1

The problem seems to be that I don't know how to tell the page to expect a list to be returned. I can't seem to declare the variables or return types properly?

I'm ignorant enough that I don't know what I am missing. The line number it refers to is the line in the controller that populates the list: res = database.query(queryString);

When I check the debug statements the list is populated correctly and like I said the list displays fine on initial page load, just not from the search button.

I don't know if it is the length or what, but no matter what I do I can't seem to paste the entire VF page into the editor and get it to show up. The two excerpts contain the problematic areas though.

My VF Page Excerpt:

<apex:pageBlock title="Search" >
    <apex:inputText value="{!searchString}" id="theSearchString" maxlength="100" size="110" /> &nbsp;
    <apex:CommandButton Id="btnSearch" action="{!getReservations}" rerender="ReservationsBlock" value="Search" /></apex:pageBlock>
<apex:pageBlock id="ReservationsBlock" title="Current Reservations">
    <apex:pageBlockTable value="{! reservations }" var="res">
        <apex:column headerValue="Reservation Name" value="{! res.Name }" /> 

My Controller:

public class ReservationListController {
    private String profileId;
    private String queryString;
    private String userId;        
    public String searchString {get; set; }

    public List<Reservation__c> getReservations() {
        profileId = userinfo.getProfileId();
        userId = userinfo.getUserId();
        if(searchString == null) {searchString = '';}
        if(searchString.length() == 0){        
            queryString = 'SELECT id, Name, copy_del1__c, UserIdCreatedBy__c, Copy_del1__r.Book__r.Author_Name__c, Copy_del1__r.Book__r.Name, Copy_del1__r.Book__r.Id '+'FROM Reservation__c ';
        }
        else{
            queryString = 'SELECT id, Name, copy_del1__c, UserIdCreatedBy__c, Copy_del1__r.Book__r.Author_Name__c, Copy_del1__r.Book__r.Name, Copy_del1__r.Book__r.Id '+'FROM Reservation__c  Where res.Copy_del1__r.Book__r.Name Like :' +  searchString;   

        }
        List<Reservation__c> res = new List<Reservation__c>();
        System.debug(queryString);
        res = database.query(queryString); 
        System.debug(res);
        //searchString = '';
        return(res);

    }
}

Best Answer

1) As the error says you cannot have the return type of an action method as anything but pagereference, so change it as it expects

    public pagereference getReservations() {
     return null;
     }

2) Since you changed your action function now create a reservation variable to be used in the vf page, instead of returning it in the action method

public List<account> reservations {get;set;}

3) Your like operator will not work so change it

queryString = 'SELECT id, Name, copy_del1__c, UserIdCreatedBy__c, Copy_del1__r.Book__r.Author_Name__c, Copy_del1__r.Book__r.Name, Copy_del1__r.Book__r.Id '+'FROM Reservation__c  Where res.Copy_del1__r.Book__r.Name Like Like \'%' + searchString+ '%\'';   

Page:

<apex:page controller="ReservationListController" >
  <apex:form >
  <apex:pageBlock title="Current User Information">
        Hello {! $Profile.Name + " " + $User.FirstName + " " + $User.LastName } <br />
        Hello ID {! $User.Id } <br />
        <apex:outputLink value="~/apex/library_user_edit?id={! $User.Id }" id="editUserLink">Edit My User Information</apex:outputLink>
    </apex:pageBlock>
  <apex:pageBlock title="Search" >
      <apex:inputText value="{!searchString}" id="theSearchString" maxlength="100" size="110" /> &nbsp;
      <apex:CommandButton Id="btnSearch" action="{!getReservations}" rerender="ReservationsBlock" value="Search" />
  </apex:pageBlock>
  <apex:pageBlock id="ReservationsBlock" title="Current Reservations">
        <apex:pageBlockTable value="{! reservations }" var="res">
            <apex:column headerValue="Reservation Name" value="{! res.Name }" />            
            <apex:column headerValue="Title">
            <apex:outputLink value="/{!res.id}"></apex:outputLink>
            </apex:column>
            <apex:column headerValue="Author" value="{! res.Name}">
            </apex:column>

        </apex:pageBlockTable>

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

controller:

   public class ReservationListController {
    private String profileId;
            private String queryString;
            private String userId;        
            public String searchString {get; set; }
            public List<Reservation__c> reservations {get;set;}
            public ReservationListController (){
            getReservations();
            }
            public pagereference getReservations() {
            profileId = userinfo.getProfileId();
            userId = userinfo.getUserId();
            system.debug('######'+searchString);
                if(searchString == null) {searchString = '';}
                if(searchString.length() == 0){        
                    queryString = 'SELECT id, Name, copy_del1__c, UserIdCreatedBy__c, Copy_del1__r.Book__r.Author_Name__c, Copy_del1__r.Book__r.Name, Copy_del1__r.Book__r.Id '+'FROM Reservation__c ';
                }
                else{
                queryString = 'SELECT id, Name, copy_del1__c, UserIdCreatedBy__c, Copy_del1__r.Book__r.Author_Name__c, Copy_del1__r.Book__r.Name, Copy_del1__r.Book__r.Id FROM Reservation__c  Where res.Copy_del1__r.Book__r.Name Like \'%' + searchString+ '%\'';   

                }
            List<Reservation__c> res = new List<Reservation__c>();
            reservations = database.query(queryString); 
        return null;
        }
    }
Related Topic