You need to sign in to do that
Don't have an account?
adrissel
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:
It is making a call to the following class:
Now, everything is working perfectly here. The value of "bpUser" is being set as desired. However, I have the following VF Component as well:
Suggestions?
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?
I think you have the right idea with your suggestion of using get/set.
Did you do try it like:
All Answers
I think you have the right idea with your suggestion of using get/set.
Did you do try it like:
Modify your code as mentioned below and use below code it'll help to access value for bpUser on your component.
Check out this link for in-depth details: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_properties.htm