1

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>
  • Can you describe what specifically about it "doesn't work"? – Mark Pond Dec 12 '13 at 18:25
  • Hi Mark, thanks for the quick response. When editing an existing record it works fine, but when creating a new one, the visibility checks in the controller are all coming up false, so nothing gets displayed, even if the product is selected in the Type_of_Product__c picklist. – Mark Masterson Dec 12 '13 at 18:27
2

I don't think you need the apex:parameter here. I'd also change the inputField value to refer to the local "record" instead of the standard controller "New_Request__c" since that's the one you refer to in your setEditVisibility method (

<apex:inputField value="{!record.Type_of_Product__c}" >
    <apex:actionSupport id="productType" event="onchange" action="{!setEditVisibility}" rerender="productInfo" immediate="TRUE" />
</apex:inputField>

Then within the setEditVisibility method, I'd remove the if/then and instead just set the parameter. Replace this...

if(record.Type_of_Product__c != null) {
    productType = record.Type_of_Product__c;
} else {
    productType = productSelection;
}

... with this...

productType = record.Type_of_Product__c;
| improve this answer | |
  • Hi Nathan, thank you for the input. Just doing these changes resulted in the visualforce error: SObject row was retrieved via SOQL without querying the requested field. I threw in a SOQL query to get the Type_of_Product_c where the ID = the record ID. However, this left me full circle, with the edit page working as expected, but fresh new records not refreshing the field set portion of the page when values from the picklist are selected. – Mark Masterson Dec 12 '13 at 19:18
  • Ok, are you sure the rerender is working properly? I've had to surround an outputPanel with another before (wrap your productInfo outputPanel with another one (no Id needed) and see what happens... – Nathan Williams Dec 12 '13 at 19:20
  • No luck on that. I also tried wrapping the picklist into a selectList versus the inputField, and I got the same behavior. Worked perfectly when editing an existing record, but trying to create a new record results in no rerendering. – Mark Masterson Dec 12 '13 at 20:58
  • I actually don't see anywhere where you define "new New_Request__c()"... There may not be an actual object it's referring to...? – Nathan Williams Dec 12 '13 at 21:47
  • Could be. I ended up getting the selectList to work, and playing with the outputPanel placement. – Mark Masterson Dec 13 '13 at 15:12
0

I changed the constructor up to get the current records value if an edit, or to put a default selection if a new record. Then put in a method for getting the picklist values to populate the selectList and update the save action to make sure Type_Of_Product__c picks up the changes made:

public NewRequestExtension(ApexPages.standardController controller){
        this.controller = controller;
        this.record = (New_Request__c)controller.getrecord();

        if(record.Type_of_Product__c != null) {
        productType = record.Type_of_Product__c;
    } else {
        productType = 'Product1'; //default value for New records
    }
 }

    public list<selectOption> getProducts(){

        list<selectOption> options = new list<selectOption>();
        list<Schema.PicklistEntry> values =
         New_Request__c.Type_of_Product__c.getDescribe().getPickListValues();

        for (Schema.PicklistEntry a : values)
      { 
         options.add(new SelectOption(a.getLabel(), a.getValue())); 
      }

      return options;
    }

    public PageReference save() {

        this.record.Type_of_Product__c = productType;
        return controller.save();
    }

Updated Visualforce page:

<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:outputPanel id="productInfo" >
            <apex:actionRegion >
                <apex:pageBlockSection title="Product Selection" >
                        <apex:selectList label="Type of Product" multiselect="false" size="1" value="{!ProductType}" >
                        <apex:selectOptions value="{!Products}" />
                    <apex:actionSupport id="productType" event="onchange" action="{!setEditVisibility}" rerender="productInfo" />
                </apex:selectList>
            </apex:pageBlockSection>
            </apex:actionRegion>
            <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>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.