Examples showing how to reference Apex variables and queries in visualforce. Also shows how to send variables from visualforce to an apex controller.
Github Repo: https://github.com/Nicholashuber/Apex-Visualforce
Simply create the visualforce page and controller and click preview after saving to see the results.
Apex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class AccountVF_controller { public String var1 { get; set; } public String var2 { get; set; } public Account accountRecord{get; set;} public Contact contactRecord{get; set;} //Since this is the constructor (the same name as the controller) it is being called first. public AccountVF_controller() { accountRecord = [SELECT Id,name FROM Account limit 1]; contactRecord = [SELECT Id,name FROM Contact WHERE AccountID = :accountRecord.id limit 1]; //please be aware that in this example the first account selected should have atleast 1 contact related to it. var1 = accountRecord.id; var2 = contactRecord.id; } } |
Visualforce page:
1 2 3 4 5 6 7 8 |
<apex:page controller="AccountVF_controller"> Variable 1: {!var1} <br/> <br/> Variable 2: {!var2} </apex:page> |