Create Visualforce page using Standard Controller and Extensions in Salesforce

In this post, I am going to tell you how to use Standard Object and Extensions in a visualforce page.

Task: When you click on an Account's record, it will display the fields data of selected record.

First of all, I am going to design a visualforce page look like this.



Function:

  1. We create a tab whose name is Account.
  2. It has two part one is Account Records and another is Account Detail as shown in the figure.
  3. In Account Records section, we display all records of the Account object.
  4. In Account Details section, we display the details of the selected Account's record.

Source code:

Visualforce page


<apex:page showHeader="false" sidebar="false" standardController="Account" extensions="AccView1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <apex:form >
      <apex:tabPanel switchType="client" selectedTab="name2" id="theTabPanel">
            <apex:tab label="Account" name="name1" id="tabOne">
                <div class='col-sm-5'>
                <apex:outputPanel layout="block" style="overflow:auto;height:550px" >
                  <apex:pageBlock >
                      <table class="table table-hover">
                            <thead>
                              <tr>
                                <th style='font-size:20px'>Account Records</th>
                              </tr>
                            </thead>
                            <tbody>
                                <apex:repeat var="a" value="{!accList}">
                                    <tr>
                                        <td>
                                          <li style='list-style:none;font-size:15px;text-decoration:none'>
                                              <apex:commandLink value="{!a.Name}" action="{!show}" reRender="ref">
                                                  <apex:param name="acIdParam" value="{!a.id}" assignTo="{!acId}"/>
                                              </apex:commandLink>
                                          </li>
                                         </td>
                                    </tr>
                                </apex:repeat>
                            </tbody>
                      </table>  
                  </apex:pageBlock>
                </apex:outputPanel>
              </div>
                  
              <div class='col-sm-7'>
                  <apex:outputPanel layout="block" style="overflow:auto;height:550px;font-size:13px;" >
                    <apex:pageBlock id="ref" title="Details">
                          <apex:pageBlockTable value="{!accDetail}" var="b">
                            <apex:column headerValue="Account Name" value="{!b.name}"/>
                                <apex:column headerValue="Phone" value="{!b.phone}"/>
                           </apex:pageBlockTable>
                    </apex:pageBlock>
                  </apex:outputPanel>
                 
              </div>
            </apex:tab>
        </apex:tabPanel>
    </apex:form>
</apex:page>


Extensions:


public with sharing class AccView1 
{
    public List<Account> accList {get;set;}
    public List<Account> accDetail {get;set;}
    public String query {get;set;}
    public String acId {get;set;}
    public String SobjectApiName {get;set;}
    public String commaSepratedFields {get;set;}
    
    public AccView1(ApexPages.StandardController controller) 
    {
        SobjectApiName = 'Account';
        commaSepratedFields = '';
        acId = '';
        
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
        
        for(String fieldName : fieldMap.keyset())
        {
            if(commaSepratedFields == null || commaSepratedFields == '')
            {
                commaSepratedFields = fieldName;
            }
            else
            {
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }
        query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName;
        accList = Database.query(query);
    }
    
    public void show()
    {
            accDetail = Database.query('SELECT '+commaSepratedFields+' FROM Account WHERE Account.id =\'' + acId+ '\' limit 1');
    }

}

Comments

Popular posts from this blog

Tree

AVL (Adelson-Velskii and Landis) Trees

How to Build REST API Using PHP