Hello friends, In this post I am going to show you, how you can convert salesforce 18 digit record id into the 15 digit record id. When you create record in salesforce 15 digit record id will be generated but when you query that record using SOQL 18 digit record id will return. In many use-cases you have use 15 digit record id so let's see how we can get that using some methods.
Convert 18 digit record id to 15 digit
To convert 18 digit record id to 15 digit we can use two methods. Both are described here with example.
Let's query one record from salesforce to see 18 digit record id.
SELECT Id,Name FROM Account limit 1
You can see in above image 18 digit record id clearly. So let's see how we can convert it in 15 digit.
1. To convert 18 digit record id to 15 digit, we can use substring function that belongs to System.String class.
Account a=[SELECT Id,Name FROM Account limit 1];
String s15digitId=String.valueof(a.Id).substring(0,15);
System.debug('15digitId^^'+s15digitId);
Output:
2. After Spring'21 release, we can now use a dedicated function that belongs to System.Id class called to15().Account a=[SELECT Id,Name FROM Account limit 1];
String s15digitId=a.Id.to15();
System.debug('15digitId^^'+s15digitId);
Output:
Now, you guys will understand the conversion of 18 digit record id to 15 digit.
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.
0 Comments
Post a Comment