4

This is my VF Page :

<apex:page standardcontroller="Contact" extensions="CreateContactController" >
<apex:sectionHeader title="Create Contact" subtitle="from Account..."/>
<apex:form >
<apex:pageblock >
<apex:pageblockbuttons location="bottom">
<apex:commandbutton action="{!save}" value="Save" />
<apex:commandbutton action="{!cancel}" value="Cancel" />
</apex:pageblockbuttons>
<apex:pageblocksection title="Personal Information">
<apex:inputfield value="{!Contact.FirstName}" />
<apex:inputfield value="{!Contact.LastName}" />
</apex:pageblocksection>
<apex:pageblocksection title="Contact Information">
<apex:inputfield value="{!Contact.Email}" />
<apex:inputfield value="{!Contact.Phone}" />
</apex:pageblocksection>
<apex:pageblocksection title="Account Information" columns="1" id="AccInfo">
<apex:inputfield value="{!Contact.AccountId}">
<apex:actionsupport action="{!renderinfo}" event="onchange" rerender="AccInfo"/>
</apex:inputfield>
<apex:inputfield value="{!Contact.Account.Phone}" />
<apex:inputfield value="{!Contact.Account.Website}" />
</apex:pageblocksection>
<apex:pageblocksection >
<apex:outputText rendered="{!called}"> <!--HERE-->
ABCDEF
</apex:outputText>
</apex:pageblocksection>
</apex:pageblock>

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

This is my controller

public with sharing class CreateContactController {

private ApexPages.StandardController ctrl;
public Boolean called{get;set;}

    public CreateContactController(ApexPages.StandardController controller) {

     ctrl = controller;
     called = false;

    }

    public void renderinfo()
    {
       System.debug('~~~~ METHOD BEGINNING');
       called = true;
       Contact c = (Contact)ctrl.getRecord();
       //c.addError('test');
       if(!String.isBlank(c.AccountId))
       {
       c.Account = [SELECT ID,Phone,Website FROM Account WHERE ID = :c.AccountId];
       }
       else
       {
        c.Account = null;
        }
        System.debug('~~~~ c.Account = '+c.Account);
        System.debug('~~~~ called = '+called);
    }

}

Issue :

There is a boolean property "called" that determines whether to render the output text or not (highlighted in the code).

Intially the text("ABCDEF") will not be displayed as I am setting the "called" to be false in the controller.

After invoking the renderinfo() method also the outputtext is not getting rendered (aka..the change in "called"'s value from "false" to "true" after the completion of renderinfo() method is not shown in the VF)

From debug logs though, I could see that "called"'s value is indeed true.

Can someone tell me why it is happening ?

1 Answer 1

6

Your action support specifies that the pageBlockSection with the id of AccInfo be rerendered. The output text element is outside of that section so it is not rerendered.

You either have to include it inside the rerendered AccInfo section:

<apex:pageblocksection title="Account Information" columns="1" id="AccInfo">
    <apex:inputfield value="{!Contact.AccountId}">
        <apex:actionsupport action="{!renderinfo}" event="onchange" rerender="AccInfo"/>
    </apex:inputfield>
    <apex:inputfield value="{!Contact.Account.Phone}" />
    <apex:inputfield value="{!Contact.Account.Website}" />
    <apex:outputText rendered="{!called}"> <!--HERE-->
        ABCDEF
    </apex:outputText>
</apex:pageblocksection>

Or give it an id and specify that it should be rerendered as well.

<apex:pageblocksection title="Account Information" columns="1" id="AccInfo">
    <apex:inputfield value="{!Contact.AccountId}">
        <apex:actionsupport action="{!renderinfo}" event="onchange" rerender="AccInfo,yourOutput"/>
    </apex:inputfield>
    <apex:inputfield value="{!Contact.Account.Phone}" />
    <apex:inputfield value="{!Contact.Account.Website}" />
</apex:pageblocksection>
<apex:pageblocksection id="yourOutput>
    <apex:outputText rendered="{!called}"> <!--HERE-->
        ABCDEF
    </apex:outputText>
</apex:pageblocksection>

Formatting the VF code helps make it more obvious.

The other thing that comes into play with this type of rendering is that if something with an id isn't on the page to begin with (i.e., its rendered condition initially evaluates to false) you cannot rerender it. The workaround for that is to include a wrapper that always displays. Something like the following:

<apex:outputPanel layout="none" id="myOutputWrapper">
    <apex:outputText rendered="{!called}"> <!--HERE-->
        ABCDEF
    </apex:outputText>
</apex:outputPanel>

And your action support:

<apex:actionsupport action="{!renderinfo}" event="onchange" rerender="AccInfo,myOutputWrapper"/>

The wrapper is always rendered, so there will be no issue if you try to rerender it.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .