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:

<apex:page standardController="Account" extensions="AccountWithOpportunities"> 
 <apex:pageblock> 
 <apex:pageBlockSection title="Account Information">
 Name: <b>{!Account.Name}</b>
 </apex:pageBlockSection>
 <apex:pageBlockSection title="Related Opportunities">
 <apex:pageBlockTable value="{!oppList}" var="r"> 
 <apex:column value="{!r.Name}"/>
 <apex:column value="{!r.StageName}"/>
 <apex:column value="{!r.CloseDate}"/> 
 </apex:pageBlockTable> 
 </apex:pageBlockSection> 
 </apex:pageblock> 
</apex:page>

Apex Code:

Public Class AccountWithOpportunities{ 
 Public List<Opportunity> 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:

https://instance_name.salesforce.com/apex/page_name?id=AccountId

AccountWithOpportunities

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

 
Author:

Ajomon Joseph AJOMON JOSEPH
Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog https://apexcoder.com
email Email me ajomon@apexcoder.com

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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