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
sfdc newbiesfdc newbie 

How to hide a picklist by selecting some value in another picklist?

I created some code like this. but i dont want to display type 2 field by default also. Could anyone please help out

<apex:page controller="ShowSectionController" tabStyle="Account">
  <apex:form >
  <Apex:actionFunction name="hideSection" action="{!hideSectionOnChange}" rerender="pg"/>

      <apex:pageBlock id="pg">
   <apex:pageBlockSection >
              <apex:inputField value="{!acc.Type}" onchange="hideSection('{!acc.Type}')"/>
         </apex:pageBlockSection>
             <apex:pageBlockSection rendered="{!(!flag)}">
             <apex:inputField value="{!acc.Type_2__c}"/>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

public class ShowSectionController
{
    public Account acc{get;set;}
    public boolean flag{get;set;}
    public ShowSectionController()
    {
        acc = new Account();
        flag = false;
      
    }
    public void hideSectionOnChange()
    {
        if(acc.Type == 'Value 1')                        
            flag = false;
        if(acc.Type != 'Value 1')
            flag = true;
    }
}
Amritesh SahuAmritesh Sahu
Hi,

Use the following code
<apex:page controller="ShowSectionController" tabStyle="Account">
  <apex:form >
  <apex:actionFunction name="hideSection" action="{!hideSectionOnChange}" rerender="pg"/>

      <apex:pageBlock id="pg">
   <apex:pageBlockSection >
              <apex:inputField value="{!acc.Type}" onchange="hideSection()"/>
         </apex:pageBlockSection>
             <apex:pageBlockSection rendered="{!!flag}">
             <apex:inputField value="{!acc.Type_2__c}"/>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

public class ShowSectionController
{
    public Account acc{get;set;}
    public boolean flag{get;set;}
    public ShowSectionController()
    {
        acc = new Account();
        flag = false;
      
    }
    public void hideSectionOnChange()
    {
        if(acc.Type == 'Value 1')                        
            flag = false;
        if(acc.Type != 'Value 1')
            flag = true;
    }
}
In the above code the picklist2 is displayed on page load, if you change the value of picklist1 to another value except 'Value 1', then the picklist2 will hide.
if you want the other way change the <apex:pageBlockSection rendered="{!!flag}"> to <apex:pageBlockSection rendered="{!flag}">

Hope this helps.
Regards.

Cloud_forceCloud_force
So as to display any component on vf page conditionaly yo can use rendered , wherein you have to bind it to a boolean flag. Setting this boolean flag true /false will make that compoentn appear and dispear.
here as below:
<apex:inputField value="{!acc.Type_2__c}" rendered="{!setbooleanflag}"/>

set 'setbooleanflag' as false initially and flip it true when you change the other picklist value.

thanks,
http://www.forcexplore.com/2014/07/wrapper-class-in-salesforce.html
sfdc newbiesfdc newbie
i have used {!!flag} iam able to see only type then no change is there even if i select value 1 type 2 picklist not displaying
sfdc newbiesfdc newbie
i have modified onchange="hideSection() and removed {!!flag} and placed {!flag} and in apex class i have changed this piece of code public void hideSectionOnChange()
    {
        if(acc.Type == 'Value 1')                        
            flag = false;
        if(acc.Type != 'Value 1')
            flag = true;
    }

Thanks for all inputs

sfdc newbiesfdc newbie
Hi Amritesh,

why this page is not present in my account layout
Amritesh SahuAmritesh Sahu
If you had used the standardcontroller as Account then this vf should be available in Account Page Layout
Like 
<apex:page standardcontroller="Account" extensions="ShowSectionController" tabStyle="Account">
sfdc newbiesfdc newbie
do i need to change anything in Apex class too
sfdc newbiesfdc newbie
it is showing some error like: Visualforce Error Help for this Page System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!hideSectionOnChange}' in page type: Class.ShowSectionController.hideSectionOnChange: line 16, column 1 Class.ShowSectionController.hideSectionOnChange: line 16, column 1
Amritesh SahuAmritesh Sahu
Yes, you should replace the controller class constructor as
public ShowSectionController(ApexPages.StandardController controller){


sfdc newbiesfdc newbie
Hi, i replaced it but it is showing me error like Error: ShowSectionController Compile Error: unexpected token: 'ShowSectionController' at line 1 column 7 public ShowSectionController(ApexPages.StandardController controller) { public Account acc{get;set;} public boolean flag{get;set;} public ShowSectionController() { acc = new Account(); flag = false; } public void hideSectionOnChange() { if(acc.Type != 'Previous Customer') flag = false; if(acc.Type == 'Previous Customer') flag = true; } }
Amritesh SahuAmritesh Sahu
public class ShowSectionController
{
    public Account acc{get;set;}
    public boolean flag{get;set;}
    public ShowSectionController(ApexPages.StandardController controller)
    {
        acc = new Account();
        flag = false;     
    }

    public void hideSectionOnChange()
    {
        if(acc.Type == 'Value 1')                       
            flag = false;
        if(acc.Type != 'Value 1')
            flag = true;
    }
}
sfdc newbiesfdc newbie
we can see this like this in detail page [image: Inline image 2] Can't we diplay this like normal fields and that to reflect in edit page to please
sfdc newbiesfdc newbie
      User-added image
we can see this like this in detail page [image: Inline image 2] Can't we diplay this like normal fields and that to reflect in edit page to please
Amritesh SahuAmritesh Sahu
You can use dependent picklist in the standard pages.
Heres the link
https://help.salesforce.com/HTViewHelpDoc?id=fields_defining_field_dependencies.htm&language=en_US
sfdc newbiesfdc newbie
actually we are using same functionality but in the same place they want to hide it for other values thats the reason i am creating this