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
Cindy NormanCindy Norman 

How do i get iCheckBox selected= to work?

I’ve tried so many ways to try to make this work but can’t get it to behave.  Other posts seem to indicate that this doesn’t work. But i really need it so here goes..

In the apex controller code MainControllerClass…
public Boolean ABCCheckbox {get; set;} 
public Boolean IsABC { 
       get{if (appl==null) return false;     
           if (appl.rating__c==‘ABC’) return true; return false;} set;}   

…startup, so not null or otherwise wonky...
ABCCheckbox           = false; 

…later & before page loads, ABCCheckbox set true…
if (appl.rating__c==‘ABC’) ABCCheckbox = true;
In the page...
<apex:page controller="MainControllerClass" >
 <apex:form id="abcForm">
 <apex:pageBlock >
  <apex:outputPanel styleClass="panelWrapper" layout="block">
…
 <apex:outputPanel id="abcDetailPanel" layout="block">
    <apex:pageblocksection id="abcDetail"> 
…            
        <apex:pageBlockSectionItem >
          <apex:outputLabel value=“Check if ABC applies?”/>
          <apex:inputCheckbox value="{!HBCheckbox}" selected="{!IsABC}"/>
       </apex:pageBlockSectionItem>
…
    </apex:pageblocksection>
  </apex:outputPanel>
...
  </apex:outputPanel>
  </apex:pageBlock>
  </apex:form>
</apex:page>
I've also tried this...
<apex:pageBlockSectionItem >
          <apex:outputLabel value=“Check if ABC applies?”>
          <apex:outputPanel layout="block">
             <apex:actionRegion >
            <apex:inputCheckbox value="{!ABCCheckbox}" selected="{!IF(appl.rating__c==‘ABC’,true,false)}" >
             </apex:actionRegion>
          </apex:outputPanel>       
       </apex:pageBlockSectionItem>
…and if i do this, it is checked but that doesn’t help except shows me “selected” *can* work sometimes…
<apex:pageBlockSectionItem >
          <apex:outputLabel value=“Check if ABC applies?”>
          <apex:outputPanel layout="block">
             <apex:actionRegion >
            <apex:inputCheckbox value="{!ABCCheckbox}" selected=“true” >
             </apex:actionRegion>
          </apex:outputPanel>       
 </apex:pageBlockSectionItem>

When i check the value of ABCCheckbox, it is indeed checked if the user checks it. I just can’t load the page with it already checked.


 
R Z KhanR Z Khan
If you want it to be checked during laoding oyu can set the variable to true in the constructor of your controller
R Z KhanR Z Khan
whats HBCheckbox? why don tu set the value to ABCCheckbox instead?
R Z KhanR Z Khan
so in your controller set ABDCHeckbox to true as default.

in your visualfroce do this
<apex:inputCheckbox value="{!ABCCheckbox}">

Remove isABC. move that getter logic to the getter of ABCCheckbox. and it should work
Cindy NormanCindy Norman
HBCheckbox: Oh...sorry...was translating code to generic code so HBCheckbox is ABCCheckbox.
 
R Z KhanR Z Khan
oh since you mvoe the getter. then in oyur constructor you would need ot set appl.rating__c=‘ABC’. don't set your ABCCheckbox.
Cindy NormanCindy Norman
Thanks RZKhan. Setting it to true and then resetting to false gets pretty messy from a logic POV. But I tried setting the default to true then uncheck it upon loading the record and page but that didn't work...Now it is just always on.
The field needs to reflect the appl.rating__c value in my object.
Cindy NormanCindy Norman
I'm fairly convinced that this isn't a logic problem. I'm quite convinced that inputCheckBox's "selected=" just plain doesn't work. Does anyone have an example of it working? If so, what am i missing? Some random outputPanel somewhere or ?
R Z KhanR Z Khan
Your checkbox will be selected the if the value of ABCCheckbox is true. you dont have to specify selected attribute
Cindy NormanCindy Norman
You would think! That's the logical conclusion/expectation. It just doesn't seem to work.
R Z KhanR Z Khan
Is your issue that it shoes wrong value on the page laod. or it doesnt change when you update your appl.rating__c value?
 
Cindy NormanCindy Norman
The checkbox will react (check and uncheck as the user commands) and i reflect the value back in the record's appl.rating__c...so the logic is good.

The problem is: when i load or reload the page with the appl record, the checkbox is unchecked even though the rating__c = true.
R Z KhanR Z Khan
did you put the logic to check for the value of you rating__c field in the get method for ABCCheckbox? if oyu are changing the value of rating__c and want the checkbox to be populated autoamtically you would need to rerender the area where checkbox is located so the new value can be reflected on your page. 

In your constructor set rating__c to 'ABC' or null depending on your requirement so you have a defautl value if necessary. then do the follwoing on your ABCCheckbox
public Boolean ABCCheckbox {get{
           if (appl!=null && appl.rating__c==‘ABC’) return true; 
            return false;} set;}


 
Cindy NormanCindy Norman
The only way the page will load with it checked is if the hardcoded selected="true" is there. You can't use a boolean like selected="{!IsABC}" or selected="{!ABCCheckbox}". And it won't work if i eliminate the "selected" part and just use the value="{!ABCCheckbox}". 
There seems to be no way to have inputCheckBox conditionally set.
 
R Z KhanR Z Khan
i worked with checkboxes quite a lot and never had an issue unles the value of my variable was nto set properly or i didnt rerender the page. you can always use JS to set the checkbox selected if you want as well. but i prefer to use native apex funcitonality if possible
Cindy NormanCindy Norman
That's what the IsABC was about. But even if i put it directly in just like your example, it doesn't matter. It will never load/reload with it reflecting the value of ABCCheckbox or, in the other examples, by referencing the rating__c.
R Z KhanR Z Khan
why do you have 2 checkboxes that do the same thing? remove isABC and jsut use ABCCheckbox. dont use selected attribute jsut value and it should work. 
Cindy NormanCindy Norman
Well, the ABCCheckbox will always have the correct value in it (and, thus, my appl.rating__c will always be correct).
But could you ever load or reload and have it checked? I'd love to see an example of that because i've tried all sorts of craziness and many days just for this silly checkbox.
Or do you just use it to grab whether the user checked it and then move on?
Cindy NormanCindy Norman
I don't use 2 checkboxes...i just gave a couple more of the obvious examples of what i tried. (again my typo of HBCheckbox should have been ABCCheckbox in my code from the orig post).
You're using logic like i do...but i'm convinced that it simply doesn't work.
R Z KhanR Z Khan
ok sohere is a sample code. I assume u submit a value of rating__c with some sort of a button
Public SOBJECT appl {get; set;}
public Boolean ABCCheckbox {get{
           if (appl!=null && appl.rating__c==‘ABC’) return true; 
            return false;} set;}

public MainCOntrollerClass(){
appl.rating__c  = 'abc';
}

//YOUR LOGIC HERE

public static void setApplication(){
   if(appl.rating__c == 'abc'){
      ABCCheckbox = true;
   }
}

In your page
 
<apex:inputFiled value="{!appl.result__c}"/>
<apex:inputCheckbox value="{!ABCCheckbox}" id='abc'/>

<apex:commandButton action="{!setApplication}" rerender="abc"/>

Modify the appl sObject name as necessary and your method that you call from the button.

So in the example above, when user fills out the result__c value and click the button, it sets ABCCheckbox value and refreshes the checkbox so the new value is shown
Cindy NormanCindy Norman
Thank you RZ i really appreciate your help.
But this concentrates on gathering whether the use checked the box or not. Which isn't the problem. That works just fine.

My problem is displaying the box correctly clicked or not clicked upon page load/reload.
(Am i missing something?)
R Z KhanR Z Khan
if oyu specify the correct value of result__c and have the logic in ABCChecker get method to check result__c value then it will display properly on page load
Cindy NormanCindy Norman
Ah yes, indeed it does. That's one way to interact with the user but it is overly complicated for the user.

Displaying my list of values isn't the problem.
Displaying a checkbox correctly is the problem.

What i mean is:
In this example, i now have 3 things for the input and display of the value rather than just a small little checkbox. rating__c is a picklist of values so i have a dropdown list. I have a checkbox that doesn't update (but WILL gather the check appropriately) and i have a submit button.

***What i need is the checkbox to be checked when appropriate. Then i don't need the rest nor clutter my screen and confuse the user.***