Multiple controller extensions communication

I have a Visualforce page with two controller extensions. Is there any way to get a reference to one of the extensions in the other one?

Answer

You can’t directly reference the other classes (extensions) in the same page, but you can make variables static, assuming they participate in the form; this requirement exists because static variables are transient (do not serialize into the view state). Simple example follows:

Page:

<apex:page standardController="Account" extensions="ext1,ext2">
<apex:form >
    <apex:inputText value="{!message}"/>
    <apex:outputText value="{!ext1message}"/>
</apex:form>
</apex:page>

Ext1:

public with sharing class ext1 {
    public static string message { get; set; }

    public ext1(ApexPages.StandardController controller) {

    }
}

Ext2:

public with sharing class ext2 {

public ext2(ApexPages.StandardController controller) {

}

public string getext1message() {
    return ext1.message;
}
}

When you type a message and press Enter, it will be echoed back from ext2’s getExt1Message() function. As you can see, you can use static variables. The caveat is that message is bound to a form element, and will be submitted each time the page posts. Without the apex:inputText element, this code wouldn’t work.

Since your comments specify that it is a boolean value that is calculated, you should probably store the results of the value into an apex:inputHidden element so that the form will correctly pass the value between extensions.

Attribution
Source : Link , Question Author : rael_kid , Answer Author : Sergej Utko

Leave a Comment