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
MycodexMycodex 

uncheck all checkboxes [SOLVED]

I have a series of checkboxes on a visualforce page I created. I have one master checkbox that when it is checked, it deselects all the checkboxes below it. Here is the code for the master checkbox:

Code:
<apex:page standardController="Opportunity" extensions="OppControllerExtension" sidebar="true">
...
<apex:pageBlockSectionItem > <apex:outputLabel value="1. Does our agent currently control the business—" for="agent_controls_business"/> <apex:inputcheckbox value="{!opportunity.agent_controls_business__c}" id="agent_controls_business"> <apex:actionSupport event="onclick" rerender="thePageBlock" action="{!deselectCheckboxes}" status="status"> </apex:actionSupport> </apex:inputcheckbox> </apex:pageBlockSectionItem>

It simply runs the actionSupport component with an action of deselectCheckboxes. My extension then fires the following class

Code:
public class OppControllerExtension {

  private final Opportunity insured;
  
  public OppControllerExtension(ApexPages.StandardController stdController){
    this.insured= (Opportunity)stdController.getRecord();
  }
  
  public PageReference deselectCheckboxes()  { 
      if (insured.Competition_True__c == true) {insured.Competition_True__c = false;}
      if (insured.Know_coverage_and_pricing_details__c == true) {insured.Know_coverage_and_pricing_details__c = false;}
      if (insured.Agent_commited__c == true) {insured.Agent_commited__c = false;}
      if (insured.agent_allow_us_in_sales_process__c == true) {insured.agent_allow_us_in_sales_process__c = false;}
      if (insured.account_moved__c == true) {insured.account_moved__c = false;}
      if (insured.XL_write_other_lines_for_insured__c == true) {insured.XL_write_other_lines_for_insured__c = false;}
      if (insured.agent_hit_ratio_True__c == true) {insured.agent_hit_ratio_True__c = false;}
      return null;
  }
}

 As you can see, this is very crude. It does not account for any new checkbox fields automatically. Is there any way I can simply the deselectCheckboxes function to loop through a series of checkboxes with an id of "checkbox1" or something similar?

I could even just ignore the if statement and go straight to setting the checkboxes as false. I was just throwing this out also because I couldn't find any way to change the value of a checkbox easily on these discussion boards.


Message Edited by Mycodex on 08-20-2008 02:52 PM
Ron HessRon Hess
Q: is there any way I can simply the deselectCheckboxes function to loop through a series of checkboxes with an id of "checkbox1" or something similar?

A:
No, it element id's are not visible in the apex code.
you would have to describe the custom sobject, then loop over each field in that description, checking the field type, then setting the value of the checkbox types to false.  this uses a describe feature that is not yet available in a production environment.

so, your code is fine and should work.

ps:
change
if (insured.Competition_True__c == true) (insured.Competition_True__c = false);
to

if (insured.Competition_True__c == true) {insured.Competition_True__c = false};

 



MycodexMycodex
Thanks for the quick reply Ron. I figured as much, but wanted to check since I'm not a programmer by any means. I was surprised how easy VisualForce was to create a new opportunity edit page. Now our business requirements are satisfied.
 
I made your suggested change and it errored on the semi-colon placement after the bracket. In case anyone references this code, make sure to put the semi-colon before the bracket as shown here:
 
if (insured.Competition_True__c == true) {insured.Competition_True__c = false;}
Mark YoungMark Young

To make things even easier, ignore the check for true and just set all values to false.  This will stop any dereferencing of null issues as well.

So:

Code:
...
insured.Competition_True__c = false;
insured.Know_coverage_and_pricing_Details__c = false;
insured.Agent_Commited__c = false;
...

 

[edit:] Oops, just saw you had already realised this.  Ignore my post :-)  Mark
 



Message Edited by Mark Young on 08-20-2008 03:15 PM
sunny522sunny522
Hi Mycodex,
          Please go through the below link for simple example.
http://salesforceglobe4u.blogspot.in/2015/12/select-all-checkbox-in-visualforce-page.html
Let me know in case of any issues.