Parent Record Related List in Visualforce Page

Using apex:relatedlist we can display a list of Salesforce records that are related to a parent record with a lookup or master-detail relationship.

Let’s take an example:

To render the visualforce page properly, you must associate the Visualforce page with a valid account record in the URL. e.g. if 0019000000nMEIG is the account ID, the resulting URL should be: https://Salesforce_instance/apex/TestRelatedListPage?id=0019000000nMEIG
Visualforce Page:

<apex:page standardcontroller="Account">
<apex:pageblock title="Account">
<apex:outputlabel value="{!account.name}"></apex:outputlabel>
</apex:pageblock>
<apex:relatedlist list="Opportunities">
</apex:relatedlist>
<apex:relatedlist list="Contacts">
</apex:relatedlist>
<apex:relatedlist list="Cases">
</apex:relatedlist>
</apex:page>

Output:
download

Salesforce: Check a String whether it is null, empty or blank using Apex

In this article I’ll demonstrate how to check a String whether it is null or empty or blank using Apex.
we can use the following methods to check a whether String is null or empty or blank:
IsBlank – It Returns true if the specified String is white space, empty (”) or null, otherwise it returns false.
IsNotBlank – It Returns true if the specified String is not white space, not empty (”) and not null, otherwise it returns false.
IsEmpty – It Returns true if the specified String is empty (”) or null, otherwise it returns false.
IsNotEmpty – Returns true if the specified String is not empty (”) and not null, otherwise it returns false.
Sample Code:


public with sharing class CheckString
{
    public String CheckIt {get;set;}
     
    public CheckString()
    {
        CheckIt = 'Biswajeet';
    }
     
    //Check IsBlank
    public Boolean CheckisBlank()
    {
        if(String.isBlank(CheckIt))
        {
            //String is not white space or not empty ('') or not null
            return false;        
        }
        else
        {
            //String is white space or empty ('') or null
            return true;
        }   
    }     
     
    //Check IsNotBlank
    public Boolean CheckIsNotBlank()
    {
        if(String.isNotBlank(CheckIt))
        {
            //String is not whitespace and not empty ('') and not null
            return true;        
        }
        else
        {
            //String is whitespace or empty ('') or null
            return false;
        }   
    }
 
    //Check IsEmpty
    public Boolean CheckIsEmpty()
    {
        if(String.isEmpty(CheckIt))
        {
            //String is not empty ('') or not null
            return false;        
        }
        else
        {
            //String is empty ('') or null
            return true;
        }   
    }  
     
    //Check IsNotEmpty
    public Boolean CheckisNotEmpty()
    {
        if(String.isNotEmpty(CheckIt))
        {
            //String is not empty ('') and not null
            return true;        
        }
        else
        {
            //String is empty ('') or blank or null
            return false;
        }   
    }     
}

Action Support in Visualforce Page

Action support component adds AJAX support to other component. It allow the component to be refreshed asynchronously by calling the controller’s method when any event occurs (like onclik, onblur etc). It also allows to rerender page sections as desired.
In this article I will demonstrate how to use actionsupport in visualforce page.
Create Apex class with following code:

Public with sharing class TestActionSupport
{
    public Integer Total {get;set;}
     
    Public TestActionSupport()
    {
        Total = 0;
    }
     
    Public void IncreaseNumber()
    {
        Total ++;
    }
}

Now create the Visualforce page:

<apex:page controller="TestActionSupport">
<apex:form>
<apex:pageblock>
<apex:pageblocksection>
<apex:outputpanel id="idPanel1"> 
<apex:outputtext value="Click here to increase number">
<apex:actionsupport action="{!IncreaseNumber}" event="onclick" rerender="idResult">
</apex:actionsupport></apex:outputtext></apex:outputpanel>
<apex:outputtext id="idResult" label="No. of Clicked:" value="{!Total}">
</apex:outputtext></apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>

In the above apex class, initially variable “Total” value is set to 0. But when we will click on visualforce page “Click here to increase number” then controller action method will be called and variable “Total” will be increase. The action support also rerender the outputText which refreshes the outputText and hence shows the new value set in controller method.

Before Click:
download

After Click:

download (1)

Difference between Custom Controller and Extension?

Custom Controller:

  • It is an Apex class that implements all of the logic for a page without leveraging a standard controller.
  • You can use only one Apex class.
  • You can Use custom controllers when you want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.

Extension:

  • A controller extension is any Apex class containing a constructor that takes a single argument of type ApexPages.
  • It provides additional functionality to a controller – either a standard controller or a custom controller.
  • You can use multiple Apex classes separated by comma.
  • Use controller extensions when:You want to leverage the built-in functionality of a standard controller but override one or more actions, such as edit, view, save, or delete.You want to add new actions.You want to build a Visualforce page that respects user permissions. Although a controller extension class executes in system mode, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead, it executes in user mode, in which permissions, field-level security, and sharing rules of the current user apply.