Category Archives: Salesforce

Standard List Controller in Visuaforce Page

Standard List controllers allow you to create Visualforce pages that can display or act on a set of records. We can access Standard List controller with the help of recordSetVar which is the attribute of apex:page component.

Note: When you use the standardController attribute on the tag, you can’t use the controller attribute at the same time.

Standard List Controller Actions:

Action Description
apex:commandButton This tag is used to create a button
apex:commandLink This tag is used to create a link.
apex:actionSupport This tag is used to make an event such as onclick and onmouseover.
apex:actionPoller It periodically calls an action.
apex:actionFunction It defines a new JavaScript function.
apex:page Calls an action when the page is loaded.

Standard List Controller Actions Methods:

Method Decription
save Inserts new records or updates existing records that have been changed. After this operation is finished, the save action returns the user to the original page, if known, or the home page.
quicksave Inserts new records or updates existing records that have been changed. Unlike the save action, quicksave does not redirect the user to another page.
list Returns a PageReference object of the standard list page, based on the most recently used list filter for that object when the filterId is not specified by the user.
cancel Aborts an edit operation. After this operation is finished, the cancel action returns the user to the page where the user originally invoked the edit.
first Displays the first page of records in the set.
last Displays the last page of records in the set.
next Displays the next page of records in the set.
previous Displays the previous page of records in the set.

Here in below example, Visualforce Page is using the standard Controller “Contact”, and recordSetVar is set to “accounts”.

<apex:page standardController="Contact" recordSetVar="contacts" tabStyle="Contact" sidebar="false">
        <apex:pageBlock title="Contacts List" id="pbConList">
            <apex:selectList value="{!filterId }" size="1">
                <apex:selectOptions value="{!listViewOptions}"/>
                <apex:actionSupport event="onchange" reRender="pbConList"/>
            <!-- Contacts List -->
            <apex:pageBlockTable value="{!contacts}" var="c" rows="5">
                <apex:column value="{!c.FirstName}"/>
                <apex:column value="{!c.LastName}"/>
                <apex:column value="{!c.Email}"/>
                <apex:column value="{!c.Phone}"/>
            <!-- Pagination -->
<table style="width: 100%">
                        Page: <apex:outputText value=" {!PageNumber} of {!CEILING(ResultSize / PageSize)}"/>

<td align="center">
                        <!-- Previous page -->
                        <!-- active -->
                        <apex:commandLink action="{!Previous}" value="Prev" rendered="{!HasPrevious}"/>
                        <!-- inactive (no earlier pages) -->
                        <apex:outputText style="color: #ccc;" value="Prev" rendered="{!NOT(HasPrevious)}"/>
                        <!-- Next page -->
                        <!-- active -->
                        <apex:commandLink action="{!Next}" value="Next" rendered="{!HasNext}"/>
                        <!-- inactive (no more pages) -->
                        <apex:outputText style="color: #ccc;" value="Next" rendered="{!NOT(HasNext)}"/>

<td align="right">
                        Records per page:
                        <apex:selectList value="{!PageSize}" size="1">
                            <apex:selectOption itemValue="5" itemLabel="5"/>
                            <apex:selectOption itemValue="20" itemLabel="20"/>
                            <apex:actionSupport event="onchange" reRender="pbConList"/>





Field Update in Salesforce Using Apex Trigger

Here in below example I’m updating a custom field “Comment__c” on Account object, based on the Annual Revenue field, using apex trigger.

Sample Code:

trigger AccountTrigger on Account(before Insert, before update){  
    for(Account acc :   {       
        if(acc.AnnualRevenue > 500000000){       
            acc.Comments__c = 'Highly revenue customer';     

Retrieve Parent Record From Child Record in Salesforce

In below example “Project” is the custom Child object, and “Student” is the custom Master object.


public class SampleController
    //Contact List Variable
    public List<Project__c> proList {get;set;}
    public SampleController(){
        proList = [SELECT Id, Name, Student__r.Name FROM Project__c LIMIT 10];

Visualforce Page:

<apex:page controller="SampleController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!proList}" var="pro">
                <apex:column value="{!pro.Name}"/>
                <apex:column value="{!pro.Student__r.Name}"/>


Apex Trigger Best Practices

One Trigger Per Object: A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts.

Logic-less Triggers: Avoid complex logic in triggers. To simplify testing and re-use, triggers should delegate to Apex classes which contain the actual execution logic.

Context-Specific Handler Methods: Create context-specific handler methods in Trigger handlers.

Bulkify your Code: Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

Avoid SOQL Queries or DML Statements inside FOR Loops: An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception.

Using Collections, Streamlining Queries, and Efficient For Loops: It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits.

Querying Large Data Sets: The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore.

Use @future Appropriately: It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found.

Avoid Hardcoding IDs: When deploying Apex code between sandbox and production environments, or installing AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail.

Consistent Naming Convention: Use a consistent naming convention including the object name (e.g. AccountTrigger).