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
tlsarna1tlsarna1 

Display Field Based on Picklist Value in Previous Field

I am kind of new to Visualforce and apex ... and am trying to create something that should be fairly simple.  Basically, I want to ask the user for the Preferred Communication Contact in a case.   If they answer with the picklist value of "Client Contact", I then want to display a field that will require them to choose the client contact.  I don't want that field to display on any other picklist value though.

 

I've looked through the boards and have figured out that I need to use a "rendered" attribute ... but no matter what I've done I can't seem to make this work.  If someone can help me with the exact coding (and the "why", so I can actually "get it" in my brain), that would be great.

 

Not sure where the update needs to be made .. the page?  The custom controller?  Both?  I've messed with the custom controller, the VF page and both ... but still can't get the field to display.  I won't post everything I've tried ... but here's what I've got so far at this moment.

 

VF Page:

<apex:page standardController="Assistance_Request__c" extensions="ARWizard" tabStyle="Assistance_Request__c">
 <style type = "text/css">
  .labelText {font-weight:bold; font-size:11px} 
  .fieldText {font-weight:bolder; font-size:12px; color: #FF0000}
  </style>

  
        <script>
    function confirmCancel(){
    var isCancel = confirm("Are you sure you wish to cancel and exit this Assistance Request?");
    if (isCancel) return true;
    return false;
    }
    </script> 
    
    <apex:sectionHeader title="Assistance Request" subtitle="Step 2 of X"/>
    
  <apex:form > 
       <apex:pageBlock id="theBlock" title="Assistance Request Creation - Request Details" mode="edit">
          <apex:pageBlockButtons >
              <apex:commandButton action="{!step1}" value="Previous"/>
              <apex:commandButton action="{!step3}" value="Next"/>
              <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/>
          </apex:pageBlockButtons>
          <apex:pageBlockSection title="Account Information" columns="2">
          <apex:outputLabel styleClass="labelText" value="Account Name"/>
          <apex:outputText styleClass="fieldText" value="{!accountName}"/>
          <apex:outputLabel styleClass="labelText" value="Global Region"/>
          <apex:outputText styleClass="fieldText" value="{!globalRegion}"/>
          </apex:pageBlockSection>
          
          <apex:pageBlockSection title="Request Information" columns="1">
          <apex:inputField value="{!AssistanceRequest.Product_Service__c}" required="True"/>
          <apex:inputField value="{!AssistanceRequest.Category__c}" required="True"/>
          <apex:inputField value="{!AssistanceRequest.Action__c}" required="True"/>
          </apex:pageBlockSection>
          
          <apex:pageBlockSection title="Case Contact Information" columns="1" id="contactInfo">
          <apex:inputField value="{!AssistanceRequest.Preferred_Communication_Contact__c}" required="True"/>
          <apex:inputField value="{!AssistanceRequest.Client_Contact__c}" rendered="{!Assistance_Request__c.Preferred_Communication_Contact__c == 'Client Contact'}"/>
          
          </apex:pageBlockSection>

      </apex:pageBlock> 
  </apex:form>
</apex:page>

 Custom Controller (do I need to do anything with this at all to make it work?)

public with sharing class ARWizard {

public ApexPages.standardController controller {get; set;}
public Assistance_Request__c assistanceRequest {get; set;}
public Account account {get;set;}
public ID accountID {get;set;}
public string globalRegion {get;set;}   
public string accountName {get;set;}

 public ARWizard (ApexPages.standardController controller) {        
 if (assistanceRequest == null)        
 assistanceRequest = new Assistance_Request__c();      
   }   
   
    public PageReference step1() {        
    return Page.arStep1;    
    }
    
     public PageReference step2() {        
     try {            
     account = [SELECT Id,Name,Global_Region__c                       
                FROM Account                       
                WHERE Id =: assistanceRequest.Account_Name__c]; 
                
                globalRegion = account.Global_Region__c;
                accountName = account.Name;   
                    
                }        
                catch(Exception e)  {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));    
     }    
     return Page.arStep2;     
     }
     
     public PageReference step3() {
     return Page.arStep3;
     }
      
  
     public PageReference save() {                
     System.debug('&&&&&&& Account=' + account);            
     try{                
     insert assistanceRequest;            
     } 
     catch (System.DmlException e) {                
     ApexPages.addMessages(e);                return null;            
     }            
     controller = new ApexPages.standardController(assistanceRequest);            
     return controller.view();        
     }
     }

 Thanks in advance!