function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
michael_hymichael_hy 

Question on VF extensions

Hi, Guys.

 So far I have an issue on VF extensions.

 

Customer Controller 1

public  with sharing class DateTimeController1 {
     
    public DateTimeController1() {}
    public DateTimeController1(DateTimeController2 controller) {}
  
    DateTime t1;
    transient DateTime t2;
    String t3 ='Test111';

    public String getT1() {
        if (t1 == null) t1 = DateTime.parse('9/11/2012 11:16 AM');
        return ('' + t1).replace(' ','%20');
    }

    public String getT2() {
        if (t2 == null) t2 = System.now();
        return '' + t2;
    }

    public String getT3() {
        return '' + t3;
    }
}

 Customer Controller 2

public  with sharing class DateTimeController2 {
    String t1{get;set;}
    String t2{get;set;}
    String t3;
       
    public DateTimeController2() {}
    
    public DateTimeController2(String t1, String t2, String t3) {
        setT3(t3);
    }
    
    public PageReference search() {
    
        system.debug('DDDDDT1:' + t1);
        system.debug('DDDDDT2:' + t2);
        system.debug('DDDDDT3:' + t3);

//String str1 = 'SELECT Id, Name, CurrentTime__c from DateTimeTest__c where CurrentTime__c = :t1';
        //List<DateTimeTest__c> L1 = Database.query(str1);
        //for (DateTimeTest__c dt1 : L1){
        //    system.debug(dt1);
       //} return null; } public void setT3(String t3){ this.t3 = t3; } }

VF page:

<apex:page id="testDateTime" showHeader="false" controller="DateTimeController2" extensions="DateTimeController1">
  

  <apex:form >
  T1: <apex:outputText id="t1" value="{!t1}"></apex:outputText><br/>
  T2: <apex:outputText id="t2" value="{!t2}"></apex:outputText><br/>
  T3: <apex:outputText id="t3" value="{!t3}"></apex:outputText><br/><br/>

    <apex:commandLink value="refresh"/><br/><br/>
    <apex:commandButton action="{!search}" value="Search the Date Time"/>
  </apex:form>
</apex:page>

 When I access this VF page. I can see the data display on the page.

But when I click the button and see the debug log.

I can see the search() method has been invoked.

But both t1, t2, and t3 are null.

Why can't set the data to controller2? 

And How to transfe the data between two Customer Controller?

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

The first getter/setter that is called for a variable is that only one called. You can't share variables across extensions that way by having multiple getters/setters by the same name (or functions, for that matter). I've not tried sharing across extensions, but I'd imagine that "static" variables should communicate effectively enough (although they will be transient and therefore not saved as part of the view state). However, that being said, it would be probably be better if you used just one extension, and had that extension inherit from the other (class "X" extends "Y"). Each extension should be able to operate on its own without any other extension being available.

All Answers

sfdcfoxsfdcfox

The first getter/setter that is called for a variable is that only one called. You can't share variables across extensions that way by having multiple getters/setters by the same name (or functions, for that matter). I've not tried sharing across extensions, but I'd imagine that "static" variables should communicate effectively enough (although they will be transient and therefore not saved as part of the view state). However, that being said, it would be probably be better if you used just one extension, and had that extension inherit from the other (class "X" extends "Y"). Each extension should be able to operate on its own without any other extension being available.

This was selected as the best answer
michael_hymichael_hy

Hi, sfdcfox

Thanks for your reply.

So far I can use both static  variables and inheritance relations to get the data.

Thanks.