Category Archives: Salesforce

Difference between multiple messaging components in Visualforce Page?

Difference between multiple messaging components in Visualforce Page

  • It is used for display a message for a specific component, such as a warning or error.
    <apex:message></apex:message>
    
  • It is used for display all messages for all components on the current page, such as a warning or error.
    <apex:messages></apex:messages>
    
  • It is used for displaying custom messages in the visualforce page using the Salesforce pattern for errors, warnings and other types of messages for a given severity.
    <apex:pagemessage></apex:pagemessage>
    
  • It is used for displays all the messages that were generated for all components on the current visualforce page, presented using the Salesforce styling.
    <apex:pagemessages></apex:pagemessages>
    

Apex Message in Visualforce Pages

Apex Messages in Salesforce are used for displaying messages such as a warning or an error in visualforce page. In this article I’ll demonstrate how to use different types of messaging options in VisualForce page.
Apex message severities are:

  • CONFIRM
  • ERROR
  • FATAL
  • INFO
  • WARNING

CONFIRM:

Visualforce Page:

<apex:page controller="SuccessMessage"> 
<apex:pagemessages> 
</apex:pagemessages> 
</apex:page> 

Apex Class:

public class SuccessMessage
{ 
   public SuccessMessage()
   {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Confirm, 'This is a success message'));
   }
} 

Confirm

 

ERROR:

Visulaforce Page:

<apex:page controller="ErrorMessages"> 
<apex:pagemessages> 
</apex:pagemessages> 
</apex:page> 

Apex Class:

public class ErrorMessages
{ 
   public ErrorMessages()
   {
     ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error, 'This is an error message'));
   }
}

Error

 

WARNING:

Visualforce Page:

<apex:page controller="WarningMessage"> 
<apex:pagemessages> 
</apex:pagemessages> 
</apex:page>

Apex Class:

public class WarningMessage
{ 
   public WarningMessage()
   {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Warning, 'This is a warning message'));
   }
} 

Warning

 

INFO:

Visualforce Page:

<apex:page controller="InfoMessage"> 
<apex:pagemessages> 
</apex:pagemessages> 
</apex:page>

Apex Class:

public class InfoMessage
{ 
   public InfoMessage()
   {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Info, 'This is an informational message'));
   }
}

Info

How to disable right click on visualforce page?

Sometimes we need to disable right click on visualforce page. Here in this article i will demonstrate how to disable right click on visualforce page using javascript.

Let’s take a simple example:
The below visualforce page is a simple entry page of “Student” custom object. Here the javascript method “RightClickDisabled”, disabled right click in form onmousedown event.

<apex:page standardcontroller="Student__c">
  <script>
      function RightClickDisabled(event){    
        if (event.button==2)
        {
            alert("Right click is not allowed");       
        }
      }
  </script>
  <apex:form onmousedown="RightClickDisabled(event)">
      <apex:pageblock title="Create Student">
          <apex:pageblockbuttons>
              <apex:commandbutton action="{!save}" value="Save">
          </apex:commandbutton></apex:pageblockbuttons>
          <apex:pageblocksection columns="1">
              <apex:inputfield value="{!Student__c.First_Name__c}">
              <apex:inputfield value="{!Student__c.Last_Name__c}">
              <apex:inputfield value="{!Student__c.Date_of_Birth__c}">
              <apex:inputfield value="{!Student__c.Address__c}">              
          </apex:inputfield></apex:inputfield></apex:inputfield></apex:inputfield></apex:pageblocksection>
      </apex:pageblock>
  </apex:form>
</apex:page>

download

download (1)

How to restrict double click on visualforce page command button?

It is so important to restrict double click on visualforce page command button. For example – In an visualforce page, there are some input fields and “Save” button. When user will click on “Save” button, record will insert in to the object. If user will double click that “Save” button at same time, then records will be create twice. In this article I will demonstrate, how to achieve this functionality.
Let’s take a simple example:

Visualforce Page:

<apex:page doctype="html-5.0" standardcontroller="Account">
    <apex:form>
        <apex:pagemessages id="messages"></apex:pagemessages>
        <apex:pageblock title="Restrict Doublle Click">
            <apex:pageblockbuttons location="both">
                 
                <apex:actionstatus id="idActionStatus">
                        <apex:facet name="stop">                  
                            <apex:commandbutton action="{!Save}" disabled="false" rerender="idActionStatus" status="idActionStatus" value="Save">
                        </apex:commandbutton></apex:facet>
                        <apex:facet name="start">
                           <apex:commandbutton disabled="true" status="idActionStatus" value="Saving...">
                        </apex:commandbutton></apex:facet>
                    </apex:actionstatus>
                   
            </apex:pageblockbuttons>
            <apex:pageblocksection collapsible="false" title="Create Account">
                <apex:inputfield required="true" value="{!Account.Name}">
                  <apex:inputfield required="true" value="{!Account.AccountNumber}">
            </apex:inputfield></apex:inputfield></apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

download

download (1)

How to create password field in visualforce page?

To create a password field in visualforce page, you can use inputsecret. InputSecret is an HTML input element of type password.
Let’s take an example:
In below visualforce page there are two input fields Name & Account Number of Account object. Here Account number is in inputsecret. The inputsecret filed of Account Number will act like a password field.

<apex:page doctype="html-5.0" standardcontroller="Account">
    <apex:form>
        <apex:pageblock mode="edit" title="Test Input Secret">
            <apex:pageblockbuttons>
                <apex:commandbutton action="{!save}" value="Save">
            </apex:commandbutton></apex:pageblockbuttons>
            <apex:pageblocksection columns="1" title="Create Account">
                <apex:inputfield required="true" value="{!account.name}">
               <apex:inputsecret required="true" value="{!account.accountNumber}">
            </apex:inputsecret></apex:inputfield></apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

download