Tag Archives: Visualforce Page

Display all the fields of a sObject using Apex & Visualforce Page

Apex Code:

public class  DisplayAllFieldsClass {
 
     public map<string,schema.sobjectfield> data {get;set;}
   
     public  DisplayAllFieldsClass (){
         data = Schema.SObjectType.BISWAJEET__Student__c.fields.getMap();
     }
}

Visualforce Page Code:

<apex:page doctype="html-5.0" showheader="false" controller="DisplayAllFieldsClass">
    <apex:datatable value="{!data}" var="d">
             <apex:column headervalue="Field Name">
                 {!d}
             </apex:column>         
         </apex:datatable>
</apex:page>

How to include one visualforce page within another?

<apex:page sidebar="false" id="TestPage">
    <p>Inner Visualforce Page Before</p>
        <apex:include pagename="TestChart"></apex:include>
    <p>Inner Visualforce Page After</p>
</apex:page>

Note: Here in above sample visualforce page “TestPage” is the main page and “TestChart” is another visulaforce page within “Testpage”.

Use of immediate attribute of commandbutton & commandlink in visualforce page.

Basically we used immediate="true" in CommandButton & CommandLink, when we don’t want our validation rules to be fired during any server request.

It is a Boolean value that specifies whether the action associated with this component should happen immediately, without processing any validation rules associated with the fields on the page. If set to true, the action happens immediately and validation rules are skipped. If not specified, this value defaults to false.

We generally use it to make functionality of “Cancel” button or “Back” button, where we don’t want validation rule to get executed. If we don’t use immediate=true then on click of cancel button also, validation rules will get executed.

Here is the sample code:

<apex:commandlink action="{!cancel}" value="Cancel" styleclass="btn" id="btnCancel" immediate="true">
</apex:commandlink>

Highlight Selected Row in a table in Visualforce Page

Here is an example to highlight the selected row in a page block table in visualforce page. To do this add the below code snippet to your visualforce page to highlight the selected row color among the rest of the rows in same page block table.

Visualforce Page:

<apex:page standardController="Account" recordSetVar="Accounts" sidebar="false" showHeader="false">  
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"/>  
    <style>
        .ui-state-active{
        border: 1px solid #fbd850;
        background: rgb(199,199,199);
        font-weight: bold;
        color: #eb8f00;
        }
    </style> 
    <script>  
    function highlightRow(row){  
        $('tr').removeClass('ui-state-active');  
        $(row).parent().addClass('ui-state-active');  
    }  
    </script>  
    <apex:form >  
        <apex:pageBlock >  
            <apex:pageBlockTable value="{!accounts}" var="a">  
                <apex:column value="{!a.Name}" onclick="highlightRow(this)"/>  
                <apex:column value="{!a.AccountNumber}" onclick="highlightRow(this)"/>
                <apex:column value="{!a.Type}" onclick="highlightRow(this)"/>  
                <apex:column value="{!a.Industry}" onclick="highlightRow(this)"/>  
                <apex:column value="{!a.Phone}" onclick="highlightRow(this)"/>  
            </apex:pageBlockTable>  
        </apex:pageBlock>  
    </apex:form>    
</apex:page>  

Output:

Note: If you want to change the Highlighted color, you can write your won style class.

Salesforce: Use Tab in Visualforce Page

Visualforce Page:

<apex:page doctype="html-5.0" standardcontroller="account" tabstyle="account">
    <apex:pageblock title="{!account.Name}">
        <apex:tabpanel id="tabpanel">
         
            <apex:tab label="Account Details" rerender="tabpanel" switchtype="ajax">
            <apex:detail relatedlist="false"></apex:detail>
            </apex:tab>
             
            <apex:tab label="Contacts" rerender="tabpanel" switchtype="ajax">
            <apex:relatedlist list="Contacts"></apex:relatedlist>
            </apex:tab>
             
            <apex:tab label="Opportunities" rerender="tabpanel" switchtype="ajax">
            <apex:relatedlist list="Opportunities"></apex:relatedlist>
            </apex:tab>
             
            <apex:tab label="Cases" rerender="tabpanel" switchtype="ajax">
             <apex:relatedlist list="Cases"></apex:relatedlist>
            </apex:tab>
             
        </apex:tabpanel>
    </apex:pageblock>
</apex:page>

Note: Please pass account id in url e.g.
https://salesforce.com/apex/TabInVisualForcePage?id=0019000001DEV5z

Output:

index