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
adrisseladrissel 

How to use a local variable in a Visualforce Page/Component?

I am trying to figure out how to take a variable that is declared and set within a method in a controller and use it in a VF page.  Here is what I have code-wise to explain what I am wanting to do.  First the main controller:

public class VfSearchController_Adam { 

	public VfSearchController_Adam(){

		Boolean bpUser;

		GetUserType gut = new GetUserType();

		bpUser = gut.isBPUser();
	}
}

It is making a call to the following class:

public class GetUserType { 
    
    public GetUserType () {}
            
    User u = [SELECT AccountId,UserType,Name FROM User WHERE  Id =: Userinfo.getUserId()];
            
    public Boolean isBPUser(){
    
        List<Account> acct = [SELECT Best_Practice_Buyer__c FROM Account WHERE  Id =:u.AccountId];
    
        return acct[0].Best_Practice_Buyer__c;
    }    
}

Now, everything is working perfectly here.  The value of "bpUser" is being set as desired.  However, I have the following VF Component as well:

<apex:component controller="VfSearchController_Adam">

	<apex:variable var="foo" value="bar" rendered="{!bpUser}">
	
		<li class="itemMenu"><a href="{!$Site.CurrentSiteUrl}">BP Center</a></li>
		
	</apex:variable>  

</apex:component>


How do I make the "bpUser" variable, which is declared and populated in a method within the VFSearchController_Adam class, available in my VF page?

I have a feeling that it just won't work the way it is in the VF component right now.  I have seen variables set at the very beginning of the main class such as:

public Boolean bpUser {get;set;}
but declaring the variable like that in the main class, not within the method, prevents the variable from being populated at all.

Suggestions?
Best Answer chosen by adrissel
Shyam BhundiaShyam Bhundia
HI,
I think you have the right idea with your suggestion of using get/set.  

Did you do try it like:

public class VfSearchController_Adam { 
        public Boolean bpUser {get; set;}
	public VfSearchController_Adam(){

		GetUserType gut = new GetUserType();

		bpUser = gut.isBPUser();
	}
}

All Answers

Shyam BhundiaShyam Bhundia
HI,
I think you have the right idea with your suggestion of using get/set.  

Did you do try it like:

public class VfSearchController_Adam { 
        public Boolean bpUser {get; set;}
	public VfSearchController_Adam(){

		GetUserType gut = new GetUserType();

		bpUser = gut.isBPUser();
	}
}

This was selected as the best answer
adrisseladrissel
I haven't tried that, no.  I guess I may be missing an essential Apex concept here.  What exactly does the {get;set;} statement do?  Where is it "getting" it from?  What is it "setting" it to?

Deepak Kumar ShyoranDeepak Kumar Shyoran
You won't be able to assess a local variable outside its context rather you have to increase the scope for that variable by declaring it Public at class level.

Modify your code as mentioned below and use below code it'll help to access value for bpUser on your component.

// Controller code

public class VfSearchController_Adam { 

public Boolean bpUser {get;set;}	
public VfSearchController_Adam(){
		GetUserType gut = new GetUserType();
		bpUser = gut.isBPUser();
	}
}

// Calling class
public class GetUserType {
    
    public Boolean isBPUser(){
        
        User u = [SELECT AccountId,UserType,Name FROM User WHERE  Id =: Userinfo.getUserId()];
        List<Account> acct = [SELECT Best_Practice_Buyer__c FROM Account WHERE  Id =:u.AccountId];
   
        return acct[0].Best_Practice_Buyer__c;
    }   
}




Shyam BhundiaShyam Bhundia
So the {get;set;} is short form of a getter and setter methods for a variable.  If the get has no implementation, then it will return its current value. You can use the set part to have some kind of transformation when setting the value.  

Check out this link for in-depth details: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_properties.htm
adrisseladrissel
So, my "public VFSearchController_Adam()" method is actually calling upon the class that it is found in?  Then setting the variables as they are found in the method to the class variables themselves?
Shyam BhundiaShyam Bhundia
so the "public VFSearchController_Adam()" is execute when the class is initialised. And yes, the variables are set as the code is executed