Monday, April 6, 2015

Logic behind getRecord()

In Visualforce you can have 4 types of controllers:

Standard Controller class has a method called "getRecord()". It returns the record that is currently in context (e.g. Account, Contact, Opportunity, Custom_Object__c etc.), based on the value of the id query string parameter in the Visualforce page URL.

You can use this method in a controller extension class of a Visualforce page to get the record for a specific object. The most important point here is that you can get data in ONLY those which are referenced in a Visualforce markup. This method helps and allow you to not SOQL query to get data from associated fields of a specific object.

For example you have a Visualforce page to show Detail View of Opportunity but you need "Stage" field of Opportunity in associated Controller Extension class to perform some activities based on the value of "Stage" field.

The easy way is to query that particular record of Opportunity with the Id query string parameter in the Visualforce page URL.

The smartest way is to use getRecord() method and <apex:outputText> tag of Visualforce with an attribute of render=false. This will allow you to get the Stage field data without query into the Opportunity object.

Now first, create an Apex class. To create an Apex class Go to, Setup > Build > Customize > Develop > Apex Classes > New

Now copy and paste below code into the Apex class editor and Save it.

Visualforce Controller Extension Code

public with sharing class OpportunityCtrlExt{

    public Opportunity opportunityRecord {get; private set;}

    public OpportunityCtrlExt(ApexPages.StandardController stdController) {
        this.opportunityRecord = (Opportunity)stdController.getRecord();
    private void doProcessing(){
        // IF Stage = Prospecting THEN 
        // disable Prospecting button and enable Closed Won and Closed Lost buttons
        if(opportunityRecord.StageName == 'Prospecting'){
             // Your logic here
        // IF Stage = Closed Won or Closed Lost THEN 
        // enable Prospecting button and disable Closed Won and Closed Lost buttons
             // Your logic here


To create a Visualforce page Go to, Setup > Build > Develop > Pages

Now copy and paste below code into the Visualforce page editor and name this Visualforce page as "OpportunityCustomButtonsPage" and Save it.

Visualforce Page Code

<apex:page standardController="Opportunity" extensions="OpportunityCtrlExt">

    <!-- This will render a standard detail page of Opportunty with all related lists -->
    <apex:detail subject="{!Opportunity.Id}" relatedList="true" title="true" showChatter="true" inlineEdit="true"  />

        This is because we do not need to query Stage field in associated Controller Extension class
        because we are using getRecord() method of Standard Controller. If you will not use below line
        then you will need to query Stage field otherwise you will get an error saying:
        SObject row was retrieved via SOQL without querying the requested field: Opportunity.StageName 
    <apex:outputText value="{!Opportunity.StageName}" rendered="false"></apex:outputText>