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.

Leave a Reply