Activity Forums Salesforce® Discussions Can we used multiple controllers on a single visualforce page ?

  • Achintya

    Member
    September 4, 2019 at 12:15 pm

    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). A 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.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos