How to Convert 18 Digit Id to 15 Digit Id using Apex Class?

convert the Id to a String and using substring(startIndex, endIndex) its possible to convert from 18 digit Id to 15 Digit Id.
String AccountId = String.valueOf(Account.Id).substring(0, 15);

To convert it to 15 digits to 18 digits then you can simply set it to an Id type variable:

Id someId = ‘001J000001eun1Q’;
Which will automatically convert it for you:

system.debug(someId); // 001J000001eun1QIAQ
Then you can simply call the getSObjectType() method on the Id variable which will return the object name:

Schema.SObjectType objectType = someId.getSObjectType();
system.debug(objectType); // Account

Salesforce ID Converter
Converting 15 digit to 18 digit Salesforce ID

What is the difference between a 15 digit and a 18 digit Salesforce ID?
A 15 digit Salesforce ID is case sensitive. Ex:  00570000001ZwTi and 00570000001ZWTI are different.
A 18 digit Salesforce ID is case in-sensitive. Ex:  00570000001ZwTiXYZ and 00570000001ZWTIXYZ are same.

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

Your email address will not be published. Required fields are marked *