[SalesForce] How to get an 18 digit ID from 15 digit ID using SOQL

I have a 15 digit Sfdc ID, but I do not know which object it belongs to.

How can I find out which objects it belongs to and the convert it to 18 digits – all using SOQL?

P.S. I was annoyed at having to do this manually every single time, so I created a small app that detects 15 digits SFDC IDs and then automatically up converts them to 18 digits.

Video: https://www.youtube.com/watch?v=1xwd9WkcG10

Github: https://github.com/rgelb/SfdcIdUpConverter

Download: https://github.com/rgelb/SfdcIdUpConverter/releases/download/1.2/SfdcIdUpConverterSetup.exe

P.S. The code no longer works with the latest rev of Salesforce. I am not planning to make any updates as I no longer work with Salesforce. If anyone wants to fork the GitHub repo, it might live on.

Best Answer

To convert it to 18 digits 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
Related Topic