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
Enrico devEnrico dev 

Pass VF component attribute to component extension

Following is my code:
VF PAGE:
<apex:page standardController="account" extensions="ForecastHierarchyEditExtension" tabStyle="Account" sidebar="false"  standardStylesheets="false">                            
    <c:ForecastHierarchyEdit recordId="{!account.id}"/>  
</apex:page>

VF COMPONENT used in above page:
<apex:component Controller="ForecastHierarchyController" extensions="ForecastHierarchyEditCompExtension">
    <apex:attribute name="recordId" description="Record Id" type="id" assignTo="{!record_Id}" required="true"/>
    <apex:form >
        <apex:selectList value="{!ForecastCustomerToDelete}" multiselect="false">
            <apex:selectOptions value="{!ForecastCustomers}"/>  
        </apex:selectList>
    </apex:form>
</apex:component>

COMPONENT CUSTOM CONTROLLER:
public class ForecastHierarchyController { 

    public account acc{get;set;}
    public id record_Id;
    public id getrecord_Id(){
         return record_Id; 
    } 

    //Following is the solution for record_Id null in the constructor
    public void setrecord_Id(id s){
        if (s!=null){
          record_Id = s;
          GetData();
        }
   }


    public ForecastHierarchyController() { 
          /*Since the constructor is called before the setter
          record_Id will always be null when the constructor is called.*/                      
          system.debug('record_Id '+record_Id);      
    }

    public void GetData(){
        acc=[select Id
            from account
            where id=:record_Id];
        system.debug('Component Controller acct '+acc);
    }
}

COMPONENT EXTENSION CONTROLLER:
public class ForecastHierarchyEditCompExtension {
    public id ForecastCustomerToDelete { get; set; }
    private final ForecastHierarchyController myReference;   
    private final Account acct;


    public ForecastHierarchyEditCompExtension (ForecastHierarchyController ForecastHierarchyCtrl) {
         this.myReference = ForecastHierarchyCtrl;
          System.debug('myReference: ' + myReference);
          acct=myReference.acc;
    }   


    public List<SelectOption> ForecastCustomers{
        get {
            List<SelectOption> ForecastCustomers = new List<SelectOption>(); 
            //here acct=null
            system.debug('Component Controller Extension acct '+acct);
            if (acct!=null){
                if (acct.id!=null){             
                    for (account acc :[select id,Name from account where Forecast_Account__c=: acct.id]){
                        ForecastCustomers.add( new SelectOption( acc.id, acc.Name ) ); 
                    }        
                } 
            }     
            return ForecastCustomers;
        }
        private set;
    } 

}

User-added image

I know that we cannot access the attribute record_Id in the constructor of the VF component controller (null value) so i m setting it with
public void setrecord_Id(id s){
How can i pass record_Id to the component extension controller ? I mean to ForecastHierarchyEditCompExtension constructor.

I would like to get populate myReference.acc or myReference.record_Id so that also List ForecastCustomers will be populated.

Actually List is not populated because acct is null.

Thanks in advantage for any advice.
 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@Enrico dev:

Try creating an other varaible in extension controller and pass the account id to it too! like:
 
<apex:component Controller="ForecastHierarchyController" extensions="ForecastHierarchyEditCompExtension"> <apex:attribute name="recordId" description="Record Id" type="id" assignTo="{!record_Id}" required="true"/> 
 <apex:attribute name="recordIdForExt" description="Record Id" type="id" assignTo="{!AccountId}" required="true"/> <apex:form > <apex:selectList value="{!ForecastCustomerToDelete}" multiselect="false"> <apex:selectOptions value="{!ForecastCustomers}"/> </apex:selectList> </apex:form> </apex:component>



Extension:

 
public class ForecastHierarchyEditCompExtension {
    public id ForecastCustomerToDelete { get; set; }
    private final ForecastHierarchyController myReference;   
    private final Account acct;
    public id AccountId;


    public ForecastHierarchyEditCompExtension (ForecastHierarchyController ForecastHierarchyCtrl) {
         this.myReference = ForecastHierarchyCtrl;
          System.debug('myReference: ' + myReference);
          acct=myReference.acc;
    }   


    public List<SelectOption> ForecastCustomers{
        get {
            List<SelectOption> ForecastCustomers = new List<SelectOption>(); 
            //here acct=null
            system.debug('Component Controller Extension acct '+acct);
            if (AccountId!=null){
                if (AccountId.id!=null){             
                    for (account acc :[select id,Name from account where Forecast_Account__c=:AccountId]){
                        ForecastCustomers.add( new SelectOption( acc.id, acc.Name ) ); 
                    }        
                } 
            }     
            return ForecastCustomers;
        }
        private set;
    } 

}


Hope it helps:

Thanks,
balaji


 
Enrico devEnrico dev
Thanks balaji for your input but adding another parameter with same value i don t think is a good solution. I´m trying to find away to share the attribute between controller and extension.
Balaji Chowdary GarapatiBalaji Chowdary Garapati
if so, you can do one more thing:

Use "static" concept:

ForecastHierarchyController:
...
public Static record_Id{get;set;}

and in extension controller refer it as:

..........

 for (account acc :[select id,Name from account whereForecast_Account__c=:ForecastHierarchyController.record_Id]){

......................

Try it!

Thanks,
Balaji