1

I have been having some issues figuring out why my ProblemWrapper selected variable is never getting set to true. If you could have a look (Condensed into two classes for ease of reading) it would be greatly appreciated. Thanks!

START UPDATE
To be more specific, I am selecting one checkbox in the visualforce page (jQuery ensures that only one checkbox can be selected at any given time) "". Once selected, I am pushing the command button "". Upon execution of the appropriate method, I am finding that the selected wrapper variable is not being updated to TRUE.
END UPDATE

Controller:

public with sharing class SearchAndLinkController 
{

  private Integer counter=0;  //keeps track of the offset
  private Integer listSize=10; //sets the page size or number of rows
  public Integer totalSize; //used to show user the total size of the list
  private String soql;
  private String id;
  private List<Case> cases;
  public List<ProblemWrapper> problems {get;set;}
  public List<BMCServiceDesk__Problem__c> selectedProblems {get;set;}

    public SearchAndLinkController() 
    {
        totalSize = [SELECT COUNT() FROM BMCServiceDesk__Problem__c]; //set the total size in the constructor
      id = System.currentpagereference().getparameters().get('id');
      cases = [SELECT Id, Problem__c FROM Case WHERE Id = :id];
        soql = 'SELECT Name, BMCServiceDesk__Priority__c, Title_Subject__c, BMCServiceDesk__Status__c, LastModifiedDate FROM BMCServiceDesk__Problem__c WHERE BMCServiceDesk__State__c = TRUE';
        runQuery();
    }

  public void runQuery()
  {
    try
    {
          List<BMCServiceDesk__Problem__c> problemList = Database.query(soql + ' order by ' + sortField +  ' ' + sortDir + ' LIMIT ' + listSize + ' OFFSET ' + counter);

          problems = new List<ProblemWrapper>();
          selectedProblems = new List<BMCServiceDesk__Problem__c>();

          for(BMCServiceDesk__Problem__c problem : problemList)
          {
              problems.add(new ProblemWrapper(problem));
          }
    }
    catch(Exception e)
    {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Exception Message = ' + e.getMessage() + ' | Stack Trace = ' + e.getStackTraceString()));
    }
  }

  public PageReference runSearch() 
  {

    String textSearch = Apexpages.currentPage().getParameters().get('textSearch');

    soql = 'SELECT Name, BMCServiceDesk__Priority__c, Title_Subject__c, BMCServiceDesk__Status__c, LastModifiedDate FROM BMCServiceDesk__Problem__c WHERE BMCServiceDesk__State__c = TRUE';
    if (!textSearch.equals(''))
      soql += ' AND (Name LIKE \'%'+String.escapeSingleQuotes(textSearch)+'%\' OR Title_Subject__c LIKE \'%' +String.escapeSingleQuotes(textSearch)+'%\' OR BMCServiceDesk__Status__c LIKE \'%' +String.escapeSingleQuotes(textSearch)+'%\')';

    // run the query again
    runQuery();

    return null;
    }

  /* Method to fetch the selected records and send email to them */
  public PageReference processSelected()
  {
      try
      {
          if (problems != null)
          {
              system.debug('problems = ' + problems);
              for(ProblemWrapper pw : problems)
              {
                  system.debug('pw.selected = ' + pw.selected);
                  if(pw.selected == true)
                  {
                      system.debug('pw.selected = true');
                      selectedProblems.add(pw.problem);
                  }
              }
              system.debug('selectedProblems = ' + selectedProblems);
              //setCaseProblem(cases, selectedProblems[0].Id);
          }
          Pagereference p=new Pagereference ('/' + id);
          return p;
      }
      catch(Exception e)
      {
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Exception Message = ' + e.getMessage() + ' | Stack Trace = ' + e.getStackTraceString()));
          return null;
      }
  }

  public void setCaseProblem(List<Case> cases, String ProblemId)
  {
      for(Case c : cases)
      {
          c.Problem__c = ProblemId;
      }
      update cases;
  }


  public void toggleSort() 
  {
    // simply toggle the direction
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
    }

public String sortDir 
{
    get 
    { 
        if (sortDir == null) 
        {  
            sortDir = 'asc'; 
        } 
        return sortDir;  
    }
    set;
}

    // the current field to sort by. defaults to last name
public String sortField 
{
    get  
    { 
        if (sortField == null) 
        {
            sortField = 'Name'; 
        } 
        return sortField; 
    }
    set;
}

    // format the soql for display on the visualforce page
public String debugSoql 
{
    get 
    { 
        return soql + ' order by ' + sortField +  ' ' + sortDir + ' LIMIT ' + listSize + ' OFFSET ' + counter; 
    }
    set;
}
//user clicked beginning
  public PageReference Beginning() 
  { 
    if(counter != 0)
    {
        counter = 0;
        runQuery(); 
    }
    else if(counter == null)
    {
        counter = 0;
    }       
        return null;
  }

    //user clicked previous button
  public PageReference Previous() 
  { 
    counter -= listSize;
    runQuery();
        return null;
  }

//user clicked next button
  public PageReference Next() 
  { 
        counter += listSize;
        runQuery();
        return null;
  }

//user clicked end
  public PageReference End() 
  { 
        counter = totalSize - math.mod(totalSize, listSize);
        runQuery();
        return null;
    }

//this will disable the previous and beginning buttons
    public Boolean getDisablePrevious() 
    {      
        if (counter>0) 
            return false; 
        else 
            return true;
    }

//this will disable the next and end buttons
    public Boolean getDisableNext() 
    { 
        if (counter + listSize < totalSize) 
            return false; 
        else 
            return true;
    }

    public Integer getTotal_size() 
    {
        return totalSize;
    }

    public Integer getPageNumber() 
    {
        return counter/listSize + 1;
    }

    public Integer getTotalPages() 
    {
        if (math.mod(totalSize, listSize) > 0) 
        {
        return totalSize/listSize + 1;
        } 
        else 
        {
        return (totalSize/listSize);
        }
    }

  public class ProblemWrapper
  {
      public BMCServiceDesk__Problem__c problem {get; set;}
      public Boolean selected {get; set;}

      /*This is the contructor method. When we create a new ProblemWrapper object we pass a
      Problem that is set to the problem property. We also set the selected value to false*/
      public ProblemWrapper(BMCServiceDesk__Problem__c problem)
      {
          this.problem = problem;
          selected = false;
      }
  }
}

Visualforce Page:

<apex:page title="Link Problem to Case" controller="SearchAndLinkController" showHeader="false" sidebar="false" readOnly="true" cache="false">
<apex:includeScript value="{!URLFOR($Resource.jQuery)}"/>
<script type="text/javascript">
    jQuery(document).ready(function()
    {
        var $chk = $('input:checkbox.myChecks'); 
        $chk.click(function()
        {
            $chk.removeAttr('checked');
            $(this).attr('checked', 'checked');
        });
    });
</script>
<script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("textSearch").value
          );
      }
</script>  
<apex:sectionHeader title="Link Problem to Case"/>
<apex:pageBlock >
   <apex:pageBlockButtons location="top" >
   <apex:form style="float:left;padding-right:20px">
        <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
            <apex:param name="textSearch" value="" />
        </apex:actionFunction>
        Text Search<input type="text" id="textSearch" onkeyup="doSearch();"/>
   </apex:form>
   <apex:outputPanel id="myButtons">
   <apex:form >
        <apex:commandButton action="{!Beginning}" title="Beginning" value="<<" disabled="{!disablePrevious}" reRender="results,myButtons,debug"/>
        <apex:commandButton action="{!Previous}" title="Previous" value="<" disabled="{!disablePrevious}" reRender="results,myButtons,debug"/>        
        <apex:commandButton action="{!Next}" title="Next" value=">" disabled="{!disableNext}" reRender="results,myButtons,debug"/>
        <apex:commandButton action="{!End}" title="End" value=">>" disabled="{!disableNext}" reRender="results,myButtons,debug"/>
        <apex:commandButton action="{!processSelected}" value="Link Case to Problem"/>    
   </apex:form>
   </apex:outputPanel>
   </apex:pageBlockButtons>

   <apex:pageBlockSection title="(Total List Size: {!total_size})" collapsible="false" columns="1">
   <apex:outputPanel id="results">
   <apex:pageMessages id="errors" />
   <apex:form >
   <apex:pageBlockTable value="{!problems}" var="p" align="center">
        <apex:column >
            <apex:inputCheckbox value="{!p.selected}" styleClass="myChecks"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Name" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!p.problem.Name}"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Priority" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="BMCServiceDesk__Priority__c" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!p.problem.BMCServiceDesk__Priority__c}"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Title / Subject" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="Title_Subject__c" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!p.problem.Title_Subject__c}"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Status" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="BMCServiceDesk__Status__c" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!p.problem.BMCServiceDesk__Status__c}"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">
                <apex:commandLink value="Last Modified Date" action="{!toggleSort}" rerender="results,debug">
                    <apex:param name="sortField" value="LastModifiedDate" assignTo="{!sortField}"/>
                </apex:commandLink>
            </apex:facet>
            <apex:outputField value="{!p.problem.LastModifiedDate}"/>
        </apex:column>
        <apex:facet name="footer">Showing Page # {!pageNumber} of {!totalPages}</apex:facet>
   </apex:pageBlockTable>
   </apex:form>  
   </apex:outputPanel>
   </apex:pageBlockSection>

   <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />           
   </apex:pageBlock>    

</apex:pageBlock>

</apex:page>
4
  • At what point do you mean they are never getting set to true? Is it during the processing of the controller methods on one of the command buttons? It might be worth commenting out that jQuery that removes the checked attribute from matching myChecks. May 14 '14 at 1:30
  • I presume the issue is when you click the commandButton associated with action {!processSelected} based on all the debug statements you have in that method - you might want to clarify your question; as a side note - the tests of a boolean == true can be simpler as in if (boolean); also I find it helpful to name your iterator var in the pageblocktable to use the same naming convention as in your controller, i.e. pw' instead of p` (which you use in the controller as a PageRef). As 1st time poster, please remove 'noise' in the code post to let us focus on the issue at hand
    – cropredy
    May 14 '14 at 1:44
  • Please indent the beginning and ending brackets in the class. They'll show up with the rest of the code that way. I would have done that, but I'm disallowed from making an edit that short.
    – Jagular
    May 14 '14 at 4:26
  • Sorry about the brief comment above. Ultimately, I am clicking a checkbox in the visualforce page. When I click the command button to execute {!processSelected} the pw.Selected value is never set to TRUE. My understanding of the wrapper class and setting the checkbox value to {!p.selected} is that the value should automatically update the selected from true to false and vice versa depending on the state of the checkbox. Any additional help would be much appreciated. May 14 '14 at 15:27
2

It looks like you have two forms on your page. One surrounds the command buttons:

   <apex:form >
        <apex:commandButton action="{!Beginning}" title="Beginning" value="<<" disabled="{!disablePrevious}" reRender="results,myButtons,debug"/>
        <apex:commandButton action="{!Previous}" title="Previous" value="<" disabled="{!disablePrevious}" reRender="results,myButtons,debug"/>        
        <apex:commandButton action="{!Next}" title="Next" value=">" disabled="{!disableNext}" reRender="results,myButtons,debug"/>
        <apex:commandButton action="{!End}" title="End" value=">>" disabled="{!disableNext}" reRender="results,myButtons,debug"/>
        <apex:commandButton action="{!processSelected}" value="Link Case to Problem"/>    
   </apex:form> 

The other surrounds the input fields.

When you click the command button it just submits whatever is in command button's containing form, so the inputs from the other form are not submitted to the server. If you want the changed checkbox values on the page to make it to the server when the command button is clicked, they need to be inside of the same form as the command button's form.

2
  • 1
    Hi Peter, this is exactly what I needed, thanks! Marked as the answer. May 14 '14 at 16:57
  • @techbusinessman - Great! Glad to help. :) May 14 '14 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.