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
Michael Merino 1Michael Merino 1 

rerender visualforce not resetting

I have a visualforce page that successfully hides a pageblocksection using the reRender attribute until a checkbox, Food Illness, is clicked.  Then I can fill in the fields in the hidden section.  If I click SAVE this all works great. 

BUT if with the hidden section shown, I reset the hidden checkbox to unchecked and change both picklists to NULL, and THEN uncheck Food Illness and SAVE; an alert in the commandbutton states the hidden fields still contain data.  And if I edit again, the original saved values are not nulled out, WHICH IS WHAT I WANT.

<apex:page standardController="Case" tabStyle="Case" id="Page" lightningStylesheets="true">
    <apex:form id="form1">   
    <apex:PageBlock >   
            <apex:pageBlockButtons >
                <apex:commandButton value="Save"  onclick="return alert({!Case.Medical_Attention_Required__c})" action="{!save}" />
                <apex:commandButton action="{!cancel}" value="Cancel" />
            </apex:pageBlockButtons>    

            <apex:pageBlockSection showHeader="true" title="Escalated" columns="1" id="escalated">

                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Risk" style="font-weight:bold" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >
                        {!$ObjectType.Case.fields.Food_Illness__c.label}
                        <apex:actionRegion >
                            <apex:inputField value="{!Case.Food_Illness__c}">
                                <apex:actionSupport event="onchange"  reRender="foodIll" />
                            </apex:inputField>
                        </apex:actionRegion>
                </apex:pageBlockSectionItem>

               <!-- hidden section- ->
                <apex:outputPanel id="foodIll">
                    <apex:pageBlockSection columns="2" id="fi" rendered="{!Case.Food_Illness__c}" title="Food Illness">
                        <apex:inputField value="{!Case.Medical_Attention_Required__c}" />
                        <apex:outputText value="" />
                        <apex:inputField value="{!Case.Illness__c}" />
                        <apex:inputField value="{!Case.Food_Preparation__c}" />
                    </apex:pageBlockSection>
                </apex:outputPanel>               
                </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Screen Shot of VF page


 
Michael Merino 1Michael Merino 1
Think I figured it out. I put code to null out the "hidden fields" in a controller save method.  New VFP and controller shown below.
<apex:page standardController="Case" tabStyle="Case" id="Page" extensions="ControllerCaseGeneral" lightningStylesheets="true">
    <apex:form id="form1">
    
    <apex:PageBlock >
    
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!saveRecord}" />
                <apex:commandButton action="{!cancel}" value="Cancel" />
            </apex:pageBlockButtons>    

            <apex:pageBlockSection showHeader="true" title="Escalated" columns="1" id="escalated">

                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Risk" style="font-weight:bold" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >
                        {!$ObjectType.Case.fields.Food_Illness__c.label}
                        <apex:actionRegion >
                            <apex:inputField value="{!Case.Food_Illness__c}">
                                <apex:actionSupport event="onchange"  reRender="foodIll" />
                            </apex:inputField>
                        </apex:actionRegion>
                </apex:pageBlockSectionItem>

                <apex:outputPanel id="foodIll">
                    <apex:pageBlockSection columns="2" id="fi" rendered="{!Case.Food_Illness__c}" title="Food Illness">
                        <apex:inputField value="{!Case.Medical_Attention_Required__c}" />
                        <apex:outputText value="" />
                        <apex:inputField value="{!Case.Illness__c}" />
                        <apex:inputField value="{!Case.Food_Preparation__c}" />
                    </apex:pageBlockSection>
                </apex:outputPanel>
                
                </apex:pageBlockSection>

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

//////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
public without sharing class ControllerCaseGeneral{

public Case c1 {get;set;}

public ControllerCaseGeneral(ApexPages.StandardController ctrl) {
        c1= (Case)ctrl.getRecord();
        system.debug('### inside constructor '+c1);        
    }

public PageReference saveRecord() {

  system.debug('### case illness1 '+c1.food_Illness__c+' '+c1.illness__c);
  // if food_illness is false, reset all hidden fields
  if(c1.food_illness__c==false)
      {
        c1.medical_attention_required__c = false;
        c1.illness__c = null;
        c1.food_Preparation__c = null;
      }
      system.debug('### case illness2 '+c1.food_Illness__c+' '+c1.illness__c);
      upsert c1;
      return new PageReference('/' + c1.Id);
    }
}