I have a requirement to build a page that displays a different set of fields based on the value in picklist that I have on the page as an inputfield. I have this working for both the detail page, and my original solution for the edit page when working with an existing record already in context. However, it does not work when creating a new record. The visibility checks all come out to false, so nothing gets displayed, even if the proper product is selected in the picklist.
Here is my latest attempt -
Controller Code:
public with sharing class NewRequestExtension {
private ApexPages.StandardController controller {get; set;}
public New_Request__c record {get; set;}
public list<New_Request__c> typeOfProduct {get; set;}
public Boolean fieldSet1Visibility {get; set;}
public Boolean fieldSet2Visibility {get; set;}
public Boolean fieldSetVisibility {get; set;}
public string productSelection {get;set;}
public string productType {get;set;}
public NewRequestExtension(ApexPages.standardController controller){
this.controller = controller;
this.record = (New_Request__c)controller.getrecord();
}
public PageReference setEditVisibility() {
fieldset1Visibility = FALSE;
fieldset2Visibility = FALSE;
fieldset3Visibility = FALSE;
if(record.Type_of_Product__c != null) {
productType = record.Type_of_Product__c;
} else {
productType = productSelection;
}
if (productType == 'Product1' {
fieldset1Visibility = TRUE;
} else if (productType == 'Product 2' {
fieldset2Visibility = TRUE;
} else if (productType == 'Product 3' {
fieldset3Visibility = TRUE;
}
return null;
}
And here is my Visualforce code:
<apex:page StandardController="New_Request__c" extensions="NewRequestExtension" action="{!setEditVisibility}" >
<apex:form >
<apex:pageBlock title="New Request Edit" mode="edit" >
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save" />
<apex:commandButton action="{!saveandnew}" value="Save And New" />
<apex:commandButton action="{!cancel}" value="Cancel" />
</apex:pageBlockButtons>
<apex:actionRegion >
<apex:pageBlockSection title="Information" >
<apex:inputField value="{!New_Request__c.Type_of_Product__c}" >
<apex:actionSupport id="productType" event="onchange" action="{!setEditVisibility}" rerender="productInfo" immediate="TRUE" >
<apex:param name="TypeSelection" value="{!New_Request__c.Type_of_Product__c}" assignTo="{!productSelection}" />
</apex:actionSupport>
</apex:inputField>
</apex:pageBlockSection>
</apex:actionRegion>
<apex:outputPanel id="productInfo" >
<apex:pageBlockSection title="Product Information" id="product1Section" rendered="{!fieldset1Visibility}" >
<apex:repeat value="{!$ObjectType.New_Request__c.FieldSets.fieldset1}" var="fieldset1">
<apex:inputField value="{!New_Request__c[fieldset1]}" />
</apex:repeat>
</apex:pageBlockSection>
<apex:pageBlockSection title="Product Information" id="product2section" rendered="{!fieldset2Visibility}" >
<apex:repeat value="{!$ObjectType.New_Request__c.FieldSets.fieldset2}" var="fieldset2">
<apex:inputField value="{!New_Request__c[fieldset2]}" />
</apex:repeat>
</apex:pageBlockSection>
<apex:pageBlockSection title="Product Information" id="product3section" rendered="{!fieldset3Visibility}" >
<apex:repeat value="{!$ObjectType.New_Request__c.FieldSets.fieldset3}" var="fieldset3">
<apex:inputField value="{!New_Request__c[fieldset3]}" />
</apex:repeat>
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>