Tag Archives: Visualforce Page

Display Standard Salesforce Help Text on Visualforce Page

<apex:page>
    <style>
        .vfHelpText a            {position:relative;}
        .vfHelpText a span       {display: none;}
        .vfHelpText a:hover span {display: block;
        position:absolute;
        top:1.25em;
        padding:2px 5px;
        left:-15em; width:15em;
        z-index:100;
        border:1px solid orange;
        background-color:#FEFDB9;
        color:black;
        }
        .helpOrb {
        background-image: url(/img/help/helpOrbs.gif);
        background-position: 0 0;
        width: 20px;
        height: 15px;
        }
        .helpButtonon .helpOrb {
        background-position: top right;
        }
        .helpButton .helpOrb {
        background-position: top left;
        }
    </style>
    
    <span class="vfHelpText">
        <apex:outputLink value="javascript:return false;">
            <img src="/s.gif" alt="" class="helpOrb" />
            <!--s.gif image is standard image no need to upload anywhere-->
            <span>{!$ObjectType.Contact.fields.AccountId.inlineHelpText}</span>
        </apex:outputLink>
    </span>
</apex:page>

Salesforce SOSL Example

  • SOSL (Salesforce Object Search Language) is a search language in Salesforce, we can search in multiple objects at same time using SOSL. In SOQL, we can query only one object at a time but in SOSL, We can search for some specified string in multiple objects at the same time.
  • SOSL query begins with the required FIND clause.
  • The search string should be at least two characters long.
  • SOSL is used if we don’t know in which object the data is present.
  • We can mention in which fields of all the sObjects,we want to search for the string specified. Suppose you have to performed search on two objects Account & Contact. Then you can mention like, for list returned with Account results only (Name, Industry) fields should be returned, and for Contacts results (firstName, lastName) should be returned.
  • We can retrieve multiple objects and field values efficiently when the objects may or may not be related to each other.
  • We can query only on fields whose data type is text, phone and Email.
  • The result of SOSL is a list of lists of sObjects.
    The returned result contains the list of sObjects in the same order as order mentioned in SOSL query.
  • If a SOSL query does not return any records for a specified sObject type, then search results include an empty List for that sObject.
  • We can use SOSL in classes but not in Triggers.
  • We cannot perform DML operation on search result of SOSL.

Here in below example, there is a input text box and a command button which will search for the entered string in two Objects Accounts and Contacts and returned result will be shown in the page block tables.

Visualforce Page:

<apex:page controller="SampleController">
    <apex:form>
        <apex:inputText value="{!searchStr}"/>
        <apex:commandButton value="Get Records Using SOSL" action="{!Search}"/>
        <br/>
        <br/>
        <apex:pageBlock title="Accounts">
            <apex:pageblockTable value="{!accList }" var="acc">
                <apex:column value="{!acc.name}"/>
                <apex:column value="{!acc.Type}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
        <apex:pageBlock title="Contacts">
            <apex:pageblockTable value="{!conList}" var="con">
                <apex:column value="{!con.name}"/>
                <apex:column value="{!con.email}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller:

public class SampleController {
    Public List<Contact> conList {get; set;}
    Public List<Account> accList {get; set;}
    Public String searchStr {get; set;}
    
    Public void Search(){
        conList = New List<contact>();
        accList = New List<account>();
        List<List <sObject>> searchList = [FIND :searchStr IN ALL FIELDS RETURNING  Account (Id,Name,Type), Contact(Name,Email)];
        accList = ((List<account>)searchList[0]);
        conList  = ((List<contact>)searchList[1]);
    }
}

Output:

Relationships Object Records in Visualforce Page

We can have a requirement to show master object record and the respective child records in a Visualforce Page. So, for that we don’t have to write code to retrieve child object record. We can show the child records without query it.

Here is an example I’m rendering a Visualforce Page as “pdf” with Account information and the respective contact records information.

Visualforce Page:

<apex:page standardController="Account" showHeader="false" renderAs="pdf">
    
    <b>Name:</b> {!Account.Name}<br/>
    <b>Account Number:</b> {!Account.AccountNumber}<br/>
    <b>Type:</b> {!Account.Type}<br/>
    <b>Industry:</b> {!Account.Industry}<br/>
    <b>Phone:</b> {!Account.Phone}<br/>
    <p/>
    
    <table width="100%" cellpadding="2" cellspacing="2">
        <tr>
            <td><b>First Name</b></td>
            <td><b>Last Name</b></td>
            <td><b>Email</b></td>
            <td><b>Phone</b></td>
        </tr>
        <apex:repeat value="{!Account.Contacts}" var="con">
            <tr>
                <td>{!con.FirstName}</td>
                <td>{!con.LastName}</td>
                <td>{!con.Email}</td>
                <td>{!con.Phone}</td>
            </tr>
        </apex:repeat>
    </table>
</apex:page>  

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:form>
        <apex:pageBlock title="Contacts List" id="pbConList">
            Filter:
            <apex:selectList value="{!filterId }" size="1">
                <apex:selectOptions value="{!listViewOptions}"/>
                <apex:actionSupport event="onchange" reRender="pbConList"/>
            </apex:selectList>
            
            <!-- 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}"/>
            </apex:pageBlockTable>
            
            <!-- Pagination -->
            
<table style="width: 100%">
                
<tr>
                    
<td>
                        Page: <apex:outputText value=" {!PageNumber} of {!CEILING(ResultSize / PageSize)}"/>
                    </td>

            
                    
                    
<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)}"/>
                        &nbsp;&nbsp;
                        <!-- 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>

                    
                    
<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"/>
                        </apex:selectList>
                    </td>

                </tr>

            </table>

        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:

How to Access Wrapper Class Variable in Visualforce Page?

Controller:

public class SampleController
{
    //Wrapper class variable
    public Wrapper obj {get;set;}
    
    //Constructor
    public SampleController(){
        obj = new Wrapper();
        obj.name = 'Biswajeet';
    }
    
    //Wrapper class
    public class Wrapper
    {
        public String name {get;set;}
    }
}

Visualforce Page:

<apex:page controller="SampleController">
    <apex:outputText value="{!obj.name}"></apex:outputText>
</apex:page>