Tag Archives: SFDC

Salesforce: How to craete TreeView in Visualforce Page?

In this article I will demonstrate how to create treeview in visualforce page.
Here in Visualforce Page displays a collapsible treeview, with Accounts and Contacts related to the account.
For this development we need JQuery plugins. Download the Jquery Plugin from here and Upload this Zip file into Static Resources with the name “JqueryTreeView”.

Create Apex class with following code:

public with sharing class TestTreeView
{
    //Wrapper class to contain the parent nodes and their child nodes
    public class WCTreeView
    {    
        public List<contact> Child {get; set;}
        Public Account Parent {get;set;}
         
        public WCTreeView(Account objAC, List<contact> objCList)
        {
            Child = objCList;
            Parent = objAC;
        }
    }
 
    Public List<wctreeview> TreeView;
     
    Public List<wctreeview> GetNodes()
    {
        TreeView = new List<wctreeview>();
        List<account> objACList = [Select Id, Name from Account];
        for (Integer i = 0; i < objACList.size(); i++)
        {
            List<contact> objCList = [Select Id, FirstName, LastName from Contact where AccountId = :objACList[i].Id];
            TreeView.add(new WCTreeView(objACList[i], objCList));
        }   
        return TreeView;
    }   
}

Now create the Visualforce page:

<apex:page controller="TestTreeView">
<!-- Include the Jquery Script files -->
<link href="{!URLFOR($Resource.JqueryTreeView,'Jquerytreeview/jquery.treeview.css')}" rel="stylesheet">
<script src="{!URLFOR($Resource.JqueryTreeView,'Jquerytreeview/jquery.js')}" type="text/javascript"></script>
<script src="{!URLFOR($Resource.JqueryTreeView,'Jquerytreeview/jquery.cookie.js')}" type="text/javascript"></script>
<script src="{!URLFOR($Resource.JqueryTreeView,'Jquerytreeview/jquery.treeview.js')}" type="text/javascript"></script>
<!-- End of Javascript files -->
<script type="text/javascript">
$(function() {
 $("#idTreeView").treeview({
 collapsed: true,
 animated: "medium",
 control:"#IdTreeControl",
 persist: "location"
 });
})
</script>
<!-- Start TreeView -->


<div id="IdTreeControl">
<a href="https://www.blogger.com/blogger.g?#"><span style="color: black;">Collapse All</span></a> | 
<a href="https://www.blogger.com/blogger.g?#"><span style="color: black;">Expand All</span></a>
</div>




<ul id="idTreeView">
<apex:repeat value="{!Nodes}" var="Pitem">


<li>
<b><apex:outputtext escape="false" style="color: black;" value="{!Pitem.Parent.Name}"></apex:outputtext></b>


<ul>
<apex:repeat value="{!Pitem.Child}" var="Citem">


<li>
<span class="formattextcon"><apex:outputtext escape="false" style="color: red;" value="{!Citem.LastName}"></apex:outputtext></span>
</li>


</apex:repeat>  
</ul>


</li>


</apex:repeat>
</ul>


<!-- End of TreeView -->      
</apex:page>

And here is the output:

download (1)

download (2)

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.