0

I am looking to pre-fill/pre-populate a checkbox on my Visualforce page based on a specific picklist condition being met.

In this example, I would like the "ClearingHouse__c" checkbox to be automatically checked on the page if the "Type__c" picklist = "Omega".

Is this possible?

 <apex:column headerValue="Type">
   <apex:inputField value="{!a.Condition.Type__c}">
      <apex:actionSupport event="onchange" rerender="table"/>
   </apex:inputField>
 </apex:column>

 <apex:column headerValue="Clearing House">
   <apex:inputCheckbox value="{!a.Condition.ClearingHouse__c}"/>
     <apex:actionSupport event="onclick" rerender="table"/>
 </apex:column>
0

Yes, you can do this in both Apex or JavaScript.

As Apex:

  <apex:actionSupport event="onchange" rerender="table" action="{!a.checkType}" />

...

public void checkType() {
  if(Condition.Type__c=='Omega') {
    Condition.ClearingHouse__c=true;
  }
}

...

(Note: the code above presumes you have a wrapper class; this method goes in the wrapper.)

As JavaScript:

...

...

<script>
function updateCheckbox(id) {
  var type = document.querySelector(`[data-record-id="${id}"]`);
  var clearinghouse = document.querySelector(`[data-update-id="${id}"]`);
  clearinghouse.checked = type.options[type.selectedIndex].value === 'Omega';
}
</script>

...

((Note: it's been a while since I've written Visualforce+JS, this might need some debugging))

Side Note:

apex:actionSupport must be inside the element it is providing support for. This might be part of your problem.

<apex:inputCheckbox value="{!a.Condition.ClearingHouse__c}">
  <apex:actionSupport event="onclick" rerender="table"/>
</apex:inputCheckbox>
| improve this answer | |
  • Thanks for the feedback sfdcfox. How does the javascript function know how to reference the correct "clearinghouse" field in Salesforce? – FWSFDC Feb 26 at 22:21
  • Also, the Apex/VF approach does not seem to be working and the inputcheckbox remains unchecked. I do have a wrapper class and here is the method public pagereference checkType() { if(Condition.Type__c == 'Omega') { Condition.Clearinghouse__c=TRUE; } return null; } – FWSFDC Feb 27 at 3:14
  • Visualforce markup: <apex:column headerValue="Type" > <apex:inputField value="{!a.Condition.Type__c}"> <apex:actionSupport event="onchange" rerender="table"/> </apex:inputField> </apex:column> <apex:column headerValue="Clearing House"> <apex:inputCheckbox value="{!a.Condition.Clearinghouse__c}"/> <apex:actionSupport event="onclick" rerender="table" action="{!checkType}"/> </apex:column> – FWSFDC Feb 27 at 3:16

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.