Salesforce record Id 15 digit 18 digit conversion

Salesforce record Id 15 digit to 18 digit

Salesforce record Id 15 digit to 18 digit. Salesforce record Id uniquely identifies each record in salesforcce. Salesforce record Id can either be 15 or 18 digit. 15 digit salesforce record  id is case sensitive and 18 digit salesforce record id is case insensitive. Every record in salesforce is identified by unique record Id in every salesforce organization.

  • 15 digit case-sensitive version is referenced in the UI and also visible in browser
  • 18 digit case-insensitive version which is referenced through the API

The last 3 digits of the 18 digit Id is a checksum of the capitalization of the first 15 characters, this Id length was created as a workaround to legacy system which were not compatible with case-sensitive Ids. The API will accept the 15 digit Id as input but will always return the 18 digit Id.
In this post I will explain the process to convert 15 digit salesforce Id to 18 digit salesforce Id.

Click for Demo

Visualforce Code:

<apex:page controller="idConvertor">
    <apex:form >
        <apex:pageBlock >
        <apex:pageMessages id="showmsg"></apex:pageMessages>
            <apex:pageBlockButtons >
                <apex:commandButton value="Convert" action="{!convert}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
            <apex:inputText value="{!inputId}" label="Input Id:"/>
            <apex:outPutText value="{!outputId}" label="Output Id:"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Code:

public with sharing class idConvertor {
    public String inputId{get;set;}
    public String outputId{get;set;}
 
    public PageReference convert(){
        outputId = convertId(inputId);
        return null;
    }
 
    String convertId(String inputId){
        string suffix = '';
        integer flags;
        try{
            for (integer i = 0; i < 3; i++) {
                flags = 0;
                for (integer j = 0; j < 5; j++) {
                    string c = inputId.substring(i * 5 + j,i * 5 + j + 1);
                    if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
                        flags = flags + (1 << j);
                    }
                }
                if (flags <= 25) {
                    suffix += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
                }else{
                    suffix += '012345'.substring(flags - 26, flags-25);
                }
            }
        }
        catch(Exception exc){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Valid 15 digit Id'));
        }
        String outputId = inputId+suffix;
        return outputId;
    }
}

For more salesforce posts refer this link 

Permanent link to this article: https://www.sfdcpoint.com/salesforce/salesforce-id-15-digit-18-digit-conversion/

1 comment

    • Adam on March 12, 2020 at 9:05 pm
    • Reply

    If you regularly convert 15 char Id’s to 18 (to use in an external app/database/API etc), I’ve created a Chrome extension to do this:
    https://chrome.google.com/webstore/detail/sf-15-to-18/cogllpmaoflgaekieefhmglbpgdgmoeg

    Enjoy!

Leave a Reply

Your email address will not be published.