Tag Archives: Visualforce Page

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

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)

Action Poller in Visualforce Page

Action poller acts as a timer in visualforce page. It sends an AJAX request to the server according to a time interval (time interval has to be specified or else it defaults to 60 seconds). Each request can result in a full or partial page update.
In this article I will demonstrate how to use actionpoller in visualforce page.

  • In the action attribute a controller method gets called. The method gets called with a frequency defined by the interval attribute which has to be greater than or equal to 5 seconds.
  • Time out can also be specified as an attribute in actionpoller. Once the time out point is reached it stops making AJAX callout to the server and controller method is no more called.

Create Apex class with following code:

Public with sharing class TestActionPoller
{
    Public  Integer Total{get;set;}
     
    Public TestActionPoller()
    {
        Total = 0;
    }
  
    Public void CountMethod()
    {
        Total++;
    }
}

Now create the Visualforce page:

<apex:page controller="TestActionPoller">
<apex:form>
<apex:outputtext id="idCount" value="Increase in every 5 seconds: {!Total}">
<apex:actionpoller action="{!CountMethod}" interval="5" rerender="idCount">
</apex:actionpoller>
</apex:outputtext></apex:form>
</apex:page>

In above visualforce page Action poller calls the method “CountMethod” every 5 seconds where the variable “Total” is updated. Rerender attribute refreshes the page block hence showing the updated value of variable “Total”.

download

Calling Visualforce Page From Javascript

Visualforce page that calls another Visualforce page upon confirming from javascript.

Visualforce Page:

<apex:page standardController="Account">
    <apex:form>
        <apex:pageBlock>
            <apex:commandButton value="Open VF Page" onclick="OpenVFPage()"/>
        </apex:pageBlock>
    
    <script>
      function OpenVFPage(){
        var isConfirm = confirm('Do you want to open a new Visualforce Page?');
        if(isConfirm)
           window.open('/apex/YourVisualForcePage');
      }
     </script>
    </apex:form>
</apex:page>

Similarly, if you want to open a visualforce page from a custom button using javascript then use following piece of code.

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var isConfirm = confirm('Do you want to open new Visualforce Page?');
if(isConfirm){
	window.parent.location.href="/apex/YourVisualforcePage";
}