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
Andy SAndy S 

How to check a checkbox based on a picklist value on VF page?

Hi All

I'm trying to check a checkbox based on a picklist field value and vice versa, I'm using a VF page with a Standard controller and an extension and some Java Script, I'm relatively new to this so please bear with me. I think I'm heading in the right direction but messing up somewhere along the way.

Please see the code snippets below and advise where I'm going wrong/how can I achieve this? The Quality_Issue__c is the picklist and Quality_Reporting_Only__c is the checkbox that needs to be checked.
Extension class
public class ClientSupportController {
    public Case ClientSupportCase {get;set;}
    public User UserID {get;set;}
    public ApexPages.StandardController controller{get;set;}
    
    
    public ClientSupportController(ApexPages.StandardController controller) {
    
        ClientSupportCase = new Case();
        this.controller = controller; 
}

public PageReference saveClientSupportCase () {
        
        try{upsert (ClientSupportCase);
            
           } catch (System.DmlException e) {
               ApexPages.addMessages(e);
               return null;
           }
        PageReference redirectSuccess = new ApexPages.StandardController(ClientSupportCase).view();
        return (redirectSuccess);
        
    }
    
    public void QualityCheckboxSelction () {
        
        System.debug('>>>>>>>>>');
        if (ClientSupportCase != null) {
            System.debug('ClientSupportCase.QualityIssue >>>>>>>>> ' + ClientSupportCase.Quality_Issue__c);
            if(ClientSupportCase.Quality_Issue__c == 'Yes') {
                ClientSupportCase.Quality_Reporting_Only__c = true ;
            } else if (ClientSupportCase.Quality_Issue__c == 'None') {
                ClientSupportCase.Quality_Reporting_Only__c = false ;
            }
        }
        
    }

}
VF Page :
<apex:page standardController="Case" extensions="ClientSupportController" showHeader="true" sidebar="true" tabStyle="Case"  title="New Case"  >
    
    
    <script type="text/javascript">
    function selectionActionQualityCheckbox(valueCheckQualityReporting) {
                
                if(valueCheckQualityReporting == false) {
                    callMethodQualityCheckbox();
                }
            }
    </script>

<apex:form >
     <apex:pageBlock title="Case Edit" >
      <apex:pageBlockButtons >
      
    <apex:commandButton value="Save" action="{!saveClientSupportCase}" />
                <apex:commandButton value="Cancel"/>
      </apex:pageBlockButtons>
         
      <apex:actionfunction name="callMethodQualityCheckbox" action="{!QualityCheckboxSelction}" reRender="id_SectionSnapshot">  
      </apex:actionFunction>
         
      <apex:actionfunction name="callMethodQualityCheckbox" action="{!QualityCheckboxSelction}"  reRender="IncidentManagement">  
      </apex:actionFunction>   

      <apex:pageBlockSection title="Case Snapshot" id="id_SectionSnapshot">
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Case Record Type" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:outputField value="{!ClientSupportCase.RecordTypeid}"  />
                </apex:pageblocksectionitem>        
          
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Case Owner" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:inputField value="{!ClientSupportCase.ownerid}" />
                </apex:pageblocksectionitem>
          
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Status" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:inputField value="{!ClientSupportCase.Status}"  />
                </apex:pageblocksectionitem>
                
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Quality Issue" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:inputField value="{!ClientSupportCase.Quality_Issue__c}" onchange="selectionActionQualityCheckbox(this.value)" id="shiva1"/>
                </apex:pageblocksectionitem>
</apex:pageBlockSection> 
          
                
<apex:pageBlockSection title="Incident Management" id="IncidentManagement">
          
                <apex:pageblocksectionitem >   
                    <apex:outputLabel value="Impact" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:inputField value="{!ClientSupportCase.Impact__c}"/>
                </apex:pageblocksectionitem>
          
                <apex:pageblocksectionitem >
                    <apex:outputPanel >
                    <apex:outputLabel value="Quality Reporting - No Assistance Needed" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:inputField value="{!ClientSupportCase.Quality_Reporting_Only__c}" onchange="selectionActionQualityCheckbox(this.value)"/>
                </apex:outputPanel>
                    </apex:pageblocksectionitem>
</apex:pageBlockSection>


 
Lisa SeilerLisa Seiler
Hi,
you have to use if like that:
IF(condition, true, false)

Here are an example for you:
IF(valueCheckQualityReporting = FALSE,
    callMethodQualityCheckbox(),
    condition is FALSE)
The "IF-condition" works in SF a little bit foreign.
"=" is for comparison

I hope I can help you a little bit,
Lisa
BALAJI CHBALAJI CH
Hi Andy,

Not sure if you can use same actionfunction/method to this functionality.

Please check below modified Controller extension and VF page.

VF Page:
<apex:page standardController="Case" extensions="ClientSupportController" showHeader="true" sidebar="true" tabStyle="Case"  title="New Case"  >
    
    
    <script type="text/javascript">
    function selectionActionQualityCheckbox(valueCheckQualityReporting) {
        alert(valueCheckQualityReporting);
        if(valueCheckQualityReporting == 1 || valueCheckQualityReporting == 0)
        {
            callMethodQualityCheckbox();
        }
        else
        {
            callMethodQualityCheckbox1();
        }
    }
    </script>
    
    <apex:form id="frm" >
        <apex:pageBlock title="Case Edit" >
            <apex:pageBlockButtons >
                
                <apex:commandButton value="Save" action="{!saveClientSupportCase}" />
                <apex:commandButton value="Cancel"/>
            </apex:pageBlockButtons>
            
            <apex:actionfunction name="callMethodQualityCheckbox" action="{!QualityCheckboxSelction}" reRender="frm" />
            <apex:actionfunction name="callMethodQualityCheckbox1" action="{!QualityCheckboxSelction1}" reRender="frm" />
            
            
            <apex:pageBlockSection title="Case Snapshot" id="id_SectionSnapshot">
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Case Record Type" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:outputField value="{!ClientSupportCase.RecordTypeid}"  />
                </apex:pageblocksectionitem>        
                
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Case Owner" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:inputField value="{!ClientSupportCase.ownerid}" />
                </apex:pageblocksectionitem>
                
                <apex:pageblocksectionitem >
                    <apex:outputLabel value="Status" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:inputField value="{!ClientSupportCase.Status}"  />
                </apex:pageblocksectionitem>
                
                <apex:pageblocksectionitem  >
                    <apex:outputLabel value="Quality Issue" style="font-weight:Bold;padding-right:14px;"   />
                    <apex:inputField value="{!ClientSupportCase.Quality_Issue__c}"  onchange="selectionActionQualityCheckbox(this.value)" id="shiva1"/>
                </apex:pageblocksectionitem>
            </apex:pageBlockSection> 
            
            
            <apex:pageBlockSection title="Incident Management" id="IncidentManagement">
                
                <apex:pageblocksectionitem >   
                    <apex:outputLabel value="Impact" style="font-weight:Bold;padding-right:14px;"  />
                    <apex:inputField value="{!ClientSupportCase.Description}"/>
                </apex:pageblocksectionitem>
                
                <apex:pageblocksectionitem >
                    <apex:outputPanel >
                        <apex:outputLabel value="Quality Reporting - No Assistance Needed" style="font-weight:Bold;padding-right:14px;"  />
                        <apex:inputField value="{!ClientSupportCase.Quality_Reporting_Only__c}" onchange="selectionActionQualityCheckbox(this.value)" />
                    </apex:outputPanel>
                </apex:pageblocksectionitem>
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Class:
public class ClientSupportController {
    public Case ClientSupportCase {get;set;}
    public User UserID {get;set;}
    public ApexPages.StandardController controller{get;set;}
    
    
    public ClientSupportController(ApexPages.StandardController controller) {
        
        ClientSupportCase = new Case();
        this.controller = controller; 
    }
    
    public PageReference saveClientSupportCase () {
        
        try{upsert (ClientSupportCase);
            
           } catch (System.DmlException e) {
               ApexPages.addMessages(e);
               return null;
           }
        PageReference redirectSuccess = new ApexPages.StandardController(ClientSupportCase).view();
        return (redirectSuccess);
        
    }
    
    public void QualityCheckboxSelction1 () {
        
        System.debug('>>>>>>>>>'+ClientSupportCase.Quality_Issue__c);
        if (ClientSupportCase != null) {
            System.debug('ClientSupportCase.QualityIssue >>>>>>>>> ' + ClientSupportCase.Quality_Issue__c);
            if(ClientSupportCase.Quality_Issue__c == 'Yes') {
                ClientSupportCase.Quality_Reporting_Only__c = true ;
            } else if (ClientSupportCase.Quality_Issue__c == Null) {
                ClientSupportCase.Quality_Reporting_Only__c = false ;
            }
        }
    }
    public void QualityCheckboxSelction () {
        
        System.debug('>>>>>>>>>'+ClientSupportCase.Quality_Issue__c);
        if (ClientSupportCase != null) {
            if(ClientSupportCase.Quality_Reporting_Only__c == true) {
                ClientSupportCase.Quality_Issue__c = 'Yes' ;
            } else if (ClientSupportCase.Quality_Reporting_Only__c == false) {
                ClientSupportCase.Quality_Issue__c = Null ;
            }
        }
        
    }
    
}

Let me know if that works for you.

Best Regards,
BALAJI​