2

I have the following controller variable {!selectedValue} that updates successfully on the page just fine when the markup next to it is commented out. However when I add the outputPanels back in, it only updates to the first value selected and stays with that value, despite changing the radio buttons.

Visualforce:

<apex:page showHeader="true" sidebar="true" controller="CloneQuoteWithOrWithoutOpp" action="{!nullOutValues}">

<apex:outputText>Choose to either create/clone a new Opportunity or link to an existing Opportunity.</apex:outputText>
<apex:form>
    <apex:selectRadio id="slctRd" dir="ltr" required="true" layout="pageDirection" value="{!selectedValue}" immediate="true">
        <strong><font color="#FF0000"><apex:actionSupport event="onchange" action="{!changeRadio}" rerender="valueId"/></font></strong>
        <apex:selectOption itemLabel="Create New/Clone" itemValue="createNew"></apex:selectOption>
        <apex:selectOption itemLabel="Link to Existing Opp" itemValue="linkToExisting"></apex:selectOption>
    </apex:selectRadio>

        <apex:outputText id="valueId" value="{!selectedValue}" />
        <!--Above text changes value when below is commented out-->
        <!--However if you uncomment below, then it updates once and stays with that value-->
        <!--
        <apex:outputPanel id="outputpanel1" rendered="{!IF(selectedValue == 'linkToExisting','true','false')}">
            <apex:outputLabel value="Opportunity to Link to"/>
            <apex:inputField value="{!dummyQuoteForLookup.OpportunityId}" label="Opportunity to Link to"/>
        </apex:outputPanel>
        <apex:outputPanel id="outputpanel2" rendered="{!IF(selectedValue == 'createNew','true','false')}">
            <apex:outputLabel value="testing2"/>
            <apex:inputField value="{!dummyQuoteForLookup.OpportunityId}" label="testing2"/>
        </apex:outputPanel>
        -->

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

Controller:

public class CloneQuoteWithOrWithoutOpp {

    public Quote quoteId {get;set;}
    public Opportunity oppToLinkTo {get;set;}
    public Boolean createNew {get;set;}
    public Boolean linkToExisting {get;set;}
    public String selectedValue {get;set;}
    public Quote dummyQuoteForLookup {get;set;}

    public CloneQuoteWithOrWithoutOpp() {
        createNew = false;
        linkToExisting = false;
        dummyQuoteForLookup = [select id, OpportunityId from Quote order by CreatedDate limit 1];
        dummyQuoteForLookup.OpportunityId = null;
    }
    public PageReference changeRadio(){
        return null;
    }

    public PageReference nullOutValues(){
        dummyQuoteForLookup.OpportunityId = null;
        return null;
    }
}
5

You need to change the code like this way

Firstly, Remove immediate=true from apex:selectRadio which stops assigning controller attribute properly.

Secondly, Wrap those outputPanels (which are conditional) within a separate panel like here myPanel and try to render this myPanel from actionSupport rerender attribute.

Thirdly, instead of using rendered="{!IF(selectedValue == 'linkToExisting','true','false')}", just use rendered="{!selectedValue == 'linkToExisting'}"

Fourthly, since you are using required=true for apex:selectRadio, better to wrap those inside apex:actionRegion which will help you to submit the page. I know you were trying to bypass this using immediate=true, which is now not needed.

Here is your changed code:

<apex:page showHeader="true" sidebar="true" controller="CloneQuoteWithOrWithoutOpp" action="{!nullOutValues}">

<apex:outputText>Choose to either create/clone a new Opportunity or link to an existing Opportunity.</apex:outputText>
    <apex:form>
        <strong><font color="#FF0000">
        <apex:actionRegion>
        <apex:selectRadio id="slctRd" dir="ltr" required="true" layout="pageDirection" value="{!selectedValue}">
            <apex:actionSupport event="onchange" action="{!changeRadio}" rerender="valueId,myPanel"/>
            <apex:selectOption itemLabel="Create New/Clone" itemValue="createNew"></apex:selectOption>
            <apex:selectOption itemLabel="Link to Existing Opp" itemValue="linkToExisting"></apex:selectOption>
        </apex:selectRadio>
        </apex:actionRegion>
        </font></strong>

            <apex:outputText id="valueId" value="{!selectedValue}" />
            <apex:outputPanel id="myPanel">
                <apex:actionRegion>
                <apex:outputPanel id="outputpanel1" rendered="{!selectedValue == 'linkToExisting'}">
                    <apex:outputLabel value="Opportunity to Link to"/>
                    <apex:inputField value="{!dummyQuoteForLookup.OpportunityId}" label="Opportunity to Link to"/>
                </apex:outputPanel>
                <apex:outputPanel id="outputpanel2" rendered="{!selectedValue == 'createNew'}">
                    <apex:outputLabel value="testing2"/>
                    <apex:inputField value="{!dummyQuoteForLookup.OpportunityId}" label="testing2"/>
                </apex:outputPanel>
                </apex:actionRegion>
            </apex:outputPanel>
    </apex:form>
</apex:page>
1
  • 1
    Thank you very much, Santanu! This worked out perfectly!! – rmarq423 Mar 19 '18 at 2:50

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.