Category Archives: Salesforce

How to use Regex using apex in Salesforce?

Regular expressions (REGEX) is a string that is used to match another string, using a specific syntax. Apex provides patterns and matchers that enable you to search text using regular expressions.

Pattern Class: A pattern is a compiled representation of a regular expression. Patterns are used by matchers to perform match operations on a character string.

Matcher Class: It allows to do further actions such as checking to see if the string matched the pattern or allows to manipulate the original string in various ways and produce a new desired one.

Let’s take one simple example:

public Boolean ValidateEmail(String emailId) 
{
    Boolean result = false;
    String emailRegex = '^[a-zA-Z0-9._|\\\\%#~`=?&/$^*!}{+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$';
    Pattern EmailPattern = Pattern.compile(emailRegex);
    Matcher EmailMatcher = EmailPattern.matcher(emailId);
    if(EmailMatcher.matches())
    {
        result = true;
    }
    return result;
}

In above example ValidateEmail is a method with emailId parameter. In ValidateEmail method there is a string variable, which contains email validation regex. Then I create matching pattern by using the pattern class with emailRegex variable. Then I do match it against the emailId. After that I do check emailId variable is in valid email format.

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)