How to query data using SOQL and display on a Visualfroce page using PageBlockTable ?

We can query data using SOQL and display on a Visualforce page.

Let’s display Account and related Opportunities by passing id of an account in the URL. We will query all Opportunities related to the Account using SOQL in Apex, and display data on Visualforce page using PageBlockTable.

Create a Visualforce page using following code:

Visualforce Page:

 Name: {!Account.Name}

Apex Code:

Public Class AccountWithOpportunities{ 
 Public List oppList {get; set;} 
 Public AccountWithOpportunities(ApexPages.StandardController ctrl){ 
 oppList = [Select Id,Name,StageName,CloseDate From Opportunity Where AccountId =: ctrl.getId() ]; 

Let’s test the page using following URL:


Where instance_name is your domain name or server name, AccountId is the Id of Account for which Opportunities are displayed.


Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog
email Email me

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s