Wednesday 25 May 2016

Replicate Salesforce Tab - Home Page Functionaliy

Often developers get the need to override the standard or custom tab home page with some custom functionality. General expectation is to not lose the existing functionality.
Here is some sample code which helps achieve the same. It is not very feature-rich but, this can be taken as a base and customized further.

I have taken Contract as example but this can be extended to any object.

Visualforce Page & Corresponding Extension:
<apex:page standardController="Contract" extensions="ContractTabExtension">
    <script>
        function openReportsPage()
        {
            window.open("/00O/o?entityType=00O", "_parent");
        }
        function openContractRecord(ctid)
        {
            window.open("/"+ctid, "_parent");
        }
    </script>
    <u><b><apex:outputText value="Contracts Home" style="font-size:1rem;margin-left:0.4%;color:green;"/></b></u>
    <apex:enhancedList type="Contract" height="250" rowsPerPage="10" id="ContractList"/>
    <br/>
    <apex:form >
        <apex:pageBlock title="Recent Contracts">
            <apex:pageblockTable var="ct" value="{!RecentlyViewedContractList}">
                <apex:column headerValue="Contract Number">              
                    <apex:commandLink value="{!ct.Name}" onclick="openContractRecord('{!ct.Id}');" />    
                </apex:column>            
                <apex:column value="{!ct.LastViewedDate}" />
                <apex:column value="{!ct.LastReferencedDate}" />
            </apex:pageblockTable>
        </apex:pageBlock>
    
        <apex:pageblock title="Reports" >
            <apex:commandLink value="Go to Reports >>" onclick="openReportsPage();" />
        </apex:pageblock>
    </apex:form>    
</apex:page>
public with sharing class ContractTabExtension {
    public list<RecentlyViewed> RecentlyViewedContractList {get; set;}
    public ContractTabExtension(ApexPages.StandardController controller) {
        RecentlyViewedContractList = new list<RecentlyViewed>();
        RecentlyViewedContractList = [SELECT Id, Name, LastViewedDate, LastReferencedDate
                                      FROM RecentlyViewed 
                                      WHERE Type IN ('Contract')
                                      ORDER BY LastViewedDate DESC];
    }

}