2

I have one Apex Class. I want to access that Apex Class variable from other controller classes. If I'll change the value from any controller, then that same value should be available to all controller.

This is my Main CLass .....

                    Class public virtual class GenericControllerClass1 {
public static integer A{get;set;}
public GenericControllerClass1(){

    System.debug('$$$$$$$$$$$$$$ Main Class%%%%%%%%%%%%%'+ A);
}

}

My First Controller is ......

public class TestControllerClass1 extends GenericControllerClass1{ 
public TestControllerClass1(){
   GenericControllerClass1.A = 998;
    System.debug('###############'+ GenericControllerClass1.A);
}
public Pagereference CallMe(){
    Return page.P2;
}

}

My Second Controller is .........

public class TestControllerClass2 extends GenericControllerClass1{
    public TestControllerClass2(){
        System.debug('================='+GenericControllerClass1.A);
    }
}      

My First Page P1......

<apex:page controller="TestControllerClass1" >
<Apex:form >
    <apex:pageBlock title="Testing For Virtual Class">
        <apex:pageBlockSection title="Input Value in Virtual Class">
            <apex:commandButton value="Redirect" action="{!CallMe}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    </Apex:form>
</apex:page>       

My Second page P2......

<apex:page controller="TestControllerClass2">
    <apex:pageBlock title="Testing For Virtual Class">
        <apex:pageBlockSection title="Output Value in Virtual Class">
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>    

My both Vf pages refering to the different Controller like ... Controller1 & Controller2.Both the controller also refer to the same Main-Class. I'm using DebugLog to check the value of 'A' of second controller. But the value itself is showing 'null' there . I want to access the value of 'A' at Controller2, that(998) I have inserted in my controller1... Where both the controller extends the Main controller variable. But in all cases value of 'A' in 'Main Class' & Second-Controller are showing null.. So how to get this value in any controller.. ???

0

4 Answers 4

2

Sounds like you are looking more for a static, persisted variable. Every time Apex code runs is done in a transaction:

An Apex transaction represents a set of operations that are executed as a single unit. All DML operations in a transaction either complete successfully, or if an error occurs in one operation, the entire transaction is rolled back and no data is committed to the database. The boundary of a transaction can be a trigger, a class method, an anonymous block of code, a Visualforce page, or a custom Web service method.

Unfortunately, to persist a value like the way you want to do it between controllers, you need to store that variable. I suggest using a Custom Setting:

Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user. All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. This data can then be used by formula fields, validation rules, Apex, and the SOAP API.

3
  • Thanx Jesse Altman for ans.. , but my Question is slightly different. Plz check again ... Commented Nov 20, 2014 at 11:10
  • Can you edit your question with all of the code rather than adding it to comments? There is an edit link under the question. Commented Nov 20, 2014 at 11:28
  • My advice is still the same. Apex is done in transactions. There is no way to store that variable between different page loads. It has to actually be stored to the database somewhere to access it. Commented Nov 20, 2014 at 11:57
1

@Jesse is right about the fact, that each call in visualforce is done through a transaction. Even states of static classes only have scope of one transaction. There are two places where you can store things between transactions, and those are:

  1. Database - Custom settings or SObjects
  2. View State - a batch of variables of your controller (its state) which are maintained on the client.

Now, since you don't want to put stuff in the database, let's look at the second option. To utilize View state to maintain state between pages, you'd have to merge your controllers along with the main class to one Controller which all the pages you need to share state between can use. This documentation reference tells you in more detail: Salesforce create wizard

Unfortunatelly, there are no other options.

EDIT: Trying to achieve the same using inheritance unfortunatelly won't work, because each controller instance creates its own new instance of parent.

0

What you could do to fix it is to use Singleton pattern(and a salesforce link). So in your main class you would make a private constructor and a public static method to get the instance. So basically only 1 instance of your main class could exist, therefore the variable that you change in one class would also change in the other.

A short example of a singleton class:

public class YourSingletonClass {
    // private static variable referencing the class
    private static YourSingletonClass instance = null;

    // The constructor is private and initializes the id of the record type
    private YourSingletonClass(){
        //init your class here
    }
    // a static method that returns the instance of the class
    public static YourSingletonClass getInstance(){
        if(instance == null) {
            instance = new YourSingletonClass();
        }
        return instance;
    }
}

And to get the instance of this class you would write

YourSingletonClass yourVariableName = YourSingletonClass.getInstance();

instead of

YourSingletonClass yourVariableName = new YourSingletonClass();
0

I faced a similar issue when making my application. Since I couldn't call the variable from one controller in the other, I found a workaround. I passed that value as a parameter in the URL. This is achievable pretty simply using the getParameters() function of the Apex PageReference. I put the value in the URL of the first page when I moved onto the second one. These get stored as Id,value just like a map. And you can then retrieve these values in the constructor of the next page. This solved the problem for me.

You must log in to answer this question.

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