Tag Archives: Force.com

Convert 15 digit Id to 18 digit Id in Salesforce apex class

In Salesforce each record Id represents a unique record within an Organisation.
These ids are in two formats for every record Id in Salesforce:

  • 15 digit case-sensitive version which is referenced in the UI (Detail pages / reports)
  • 18 digit case-insensitive version which is referenced through the API

Apex Class:

public class TestPage
{
    public String FifteenDigit {get;set;}
    public Id EighteenDigit {get;set;}
     
    public TestPage(){
        FifteenDigit = 'a0390000009ooJG';
        EighteenDigit = FifteenDigit;
    }
}

Visualforce Page:

<apex:page controller="TestPage" doctype="html-5.0">
   <apex:form>
       <apex:outputtext value="Fifteen Digit : "></apex:outputtext>
       <apex:outputtext value="{!FifteenDigit}"></apex:outputtext>
       <apex:outputtext value="Eighteen Digit : "></apex:outputtext>
       <apex:outputtext value="{!EighteenDigit}"></apex:outputtext>
   </apex:form>
</apex:page>

download

Note: CASESAFEID(Id) function in a formula field can be used to capture the 18 digit id of a record.

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)