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
profile_nameprofile_name 

Variable is not visible in its own setter method

Hello

The requirement here is to make 2 picklists, one with records from object Functional_Group_del__c and another with records from object. Equipment_Type__c (which has a lookup field to Functional_Group_del__c)  When a record of Functional_Group_del__c is chosen in the first picklist, the values in the second picklist are restricted to records in Equipment_Type__c whose lookup field value is equal to the Functional Group chosen in the first picklist.

The variable 'fgOptions'  populates the first picklist with Functional Group values. The variable 'fgVal' must store the value currently chosen in the first picklist. 

The following code gives the error "variable is not visible" regarding 'fgVal'. Should the setter method be in some other form? In general, what must be done to give getter methods access to variables that take their values from a user input?

The markup:

<apex:page controller="buildTypeController1"> 
  <apex:pageBlock title="Select Functional Group">    
      <apex:form >
          <apex:selectList size="1" value="{!fgVal}"> 
              <apex:selectoptions value="{!fgOptions}"/>
                  <apex:actionsupport action="{!setFgVal}" event="onchange" rerender="type"/>
          </apex:selectList>
      </apex:form>
  </apex:pageBlock> 
  <apex:outputpanel id="type">
  <apex:pageBlock title="Select Equipment Type">    
      <apex:form >
          <apex:selectList size="1">
              <apex:selectoptions value="{!typeOptions}" />
          </apex:selectList>
      </apex:form>
   </apex:pageBlock>
   </apex:outputpanel>
</apex:page>

 The controller:

public with sharing class buildTypeController1 {
    public SelectOption fgVal{get;}
    public void setFgVal(SelectOption fgVal){
       system.debug('#############'+fgVal);
       this.fgVal=fgVal;
    }
    public list<SelectOption> getFgOptions(){
        list<Functional_Group_del__c> fgList = new list<Functional_Group_del__c>();
        list<SelectOption> fgOptions = new list<SelectOption>();
        fgList = [select id,name from Functional_Group_del__c];
        if(fgList.size()>0){
            for(Functional_Group_del__c fg : fgList){
                fgOptions.add(new SelectOption(fg.Name,fg.Name));
            }
            return fgOptions;
        }
        else
        return null;
    }
    public list<SelectOption> getTypeOptions(){
        list<Equipment_Type__c> typeList = new list<Equipment_Type__c>();
        list<SelectOption> typeOptions = new list<SelectOption>();
        //system.debug('*************'+string.valueof(fgVal));
        typeList = [select id,name,Functional_Group__c from Equipment_Type__c];
        if(typeList.size()>0){
            for(Equipment_Type__c et : typeList){
                typeOptions.add(new SelectOption(et.Name,et.Name));
            }
            return typeOptions;
        }
        else
        return null;
    }
        
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

 

fgVal should be a String. 

 

Replace the following section 

 

public void setFgVal(SelectOption fgVal){

system.debug('#############'+string.valueof(fgVal));
this.fgVal=fgVal;
}
public SelectOption fgVal;
public SelectOption getFgVal(){
return fgVal;
}

 

with

 

public String fgVal { get; set; }

 

 

All Answers

Avidev9Avidev9
Whenever you are defining either setter or getter, please define both explicitly
1.public void setFgVal(SelectOption fgVal)
2.public SelectOption getFgVal()
profile_nameprofile_name

Thank you. I tried that. It's giving the error: 'Method must define a body.'

Avidev9Avidev9
the actual method should be

public SelectOption getFgVal(){
return this. fgVal;
}
profile_nameprofile_name

Thank you. The code is compiling correctly. In trying to debug it, I found that the setter method is not running(ie. system.debug(''#############'+fgVal) does not appear in the debug logs). Does it not run whenever a new value is chosen in the list? Elsewhere, another debug message proves that fgVal=null.
Markup:

<apex:page controller="buildTypeController1"> 
  <apex:pageBlock title="Select Functional Group">    
      <apex:form id="fgscreen">
          <apex:selectList size="1" value="{!fgVal}">
              <apex:selectoptions value="{!fgOptions}"/>
                  <apex:actionsupport event="onchange" rerender="type,fgscreen" />
          </apex:selectList>
      </apex:form>
  </apex:pageBlock> 
  <apex:outputpanel id="type">
  <apex:pageBlock title="Select Equipment Type">    
      <apex:form >
          <apex:selectList size="1">
              <apex:selectoptions value="{!typeOptions}" />
          </apex:selectList>
      </apex:form>
   </apex:pageBlock>
   </apex:outputpanel>
</apex:page>

 Controller:

public with sharing class buildTypeController1 {
    public void setFgVal(SelectOption fgVal){
       system.debug('#############'+string.valueof(fgVal));
       this.fgVal=fgVal;
    }
    public SelectOption fgVal;
    public SelectOption getFgVal(){
        return fgVal;
    }
    
    public list<SelectOption> getFgOptions(){
        list<Functional_Group_del__c> fgList = new list<Functional_Group_del__c>();
        list<SelectOption> fgOptions = new list<SelectOption>();
        fgList = [select id,name from Functional_Group_del__c];
        if(fgList.size()>0){
            for(Functional_Group_del__c fg : fgList){
                fgOptions.add(new SelectOption(fg.Name,fg.Name));
            }
            return fgOptions;
        }
        else
        return null;
    }
    public list<SelectOption> getTypeOptions(){
        list<Equipment_Type__c> typeList = new list<Equipment_Type__c>();
        list<SelectOption> typeOptions = new list<SelectOption>();
        system.debug('*************'+string.valueof(fgVal));
        typeList = [select id,name,Functional_Group__c from Equipment_Type__c];
        if(typeList.size()>0){
            for(Equipment_Type__c et : typeList){
                typeOptions.add(new SelectOption(et.Name,et.Name));
            }
            return typeOptions;
        }
        else
        return null;
    }
        
}

 



Kiran  KurellaKiran Kurella

 

fgVal should be a String. 

 

Replace the following section 

 

public void setFgVal(SelectOption fgVal){

system.debug('#############'+string.valueof(fgVal));
this.fgVal=fgVal;
}
public SelectOption fgVal;
public SelectOption getFgVal(){
return fgVal;
}

 

with

 

public String fgVal { get; set; }

 

 

This was selected as the best answer
profile_nameprofile_name

Thank you.