Hi All,
As we know that each record Id represents a unique record within an organisation. There are two versions of every record Id in salesforce :
The API will accept the 15 digit ID as input but will always return the 18 digit ID.
Now how we can calculate the 18 Digit Id from 15 Digit Id :
//Our 15 Digit Id
String id = '00570000001ZwTi' ;
string suffix = '';
integer flags;
for (integer i = 0; i < 3; i++) {
flags = 0;
for (integer j = 0; j < 5; j++) {
string c = id.substring(i * 5 + j,i * 5 + j + 1);
//Only add to flags if c is an uppercase letter:
if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
flags = flags + (1 << j);
}
}
if (flags <= 25) {
suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
}else{
suffix = suffix + '012345'.substring(flags-25,flags-24);
}
}
//18 Digit Id with checksum
System.debug(' ::::::: ' + id + suffix) ;
Thanks
As we know that each record Id represents a unique record within an organisation. There are two versions of every record Id in salesforce :
- 15 digit case-sensitive version which is referenced in the UI
- 18 digit case-insensitive version which is referenced through the API
The API will accept the 15 digit ID as input but will always return the 18 digit ID.
Now how we can calculate the 18 Digit Id from 15 Digit Id :
//Our 15 Digit Id
String id = '00570000001ZwTi' ;
string suffix = '';
integer flags;
for (integer i = 0; i < 3; i++) {
flags = 0;
for (integer j = 0; j < 5; j++) {
string c = id.substring(i * 5 + j,i * 5 + j + 1);
//Only add to flags if c is an uppercase letter:
if (c.toUpperCase().equals(c) && c >= 'A' && c <= 'Z') {
flags = flags + (1 << j);
}
}
if (flags <= 25) {
suffix = suffix + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.substring(flags,flags+1);
}else{
suffix = suffix + '012345'.substring(flags-25,flags-24);
}
}
//18 Digit Id with checksum
System.debug(' ::::::: ' + id + suffix) ;
Thanks
so are salesforce creators are so stupid that they can't include your code as standart function somewhere?
ReplyDeleteThere is a bug in this algorithm:
ReplyDeletesuffix = suffix + '012345'.substring(flags-25,flags-24);
Correct version:
suffix += '012345'.substring(flags - 26, flags-25);
I suggest implementing a 15-to-18-character ID converter through a formula field. No code needed! http://tinyurl.com/15CharFix
ReplyDeleteId &
MID(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",
MIN(FIND(MID(Id, 5, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 16 +
MIN(FIND(MID(Id, 4, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 8 +
MIN(FIND(MID(Id, 3, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 4 +
MIN(FIND(MID(Id, 2, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 2 +
MIN(FIND(MID(Id, 1, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 1 + 1,
1) &
MID(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",
MIN(FIND(MID(Id, 10, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 16 +
MIN(FIND(MID(Id, 9, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 8 +
MIN(FIND(MID(Id, 8, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 4 +
MIN(FIND(MID(Id, 7, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 2 +
MIN(FIND(MID(Id, 6, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 1 + 1,
1) &
MID(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345",
MIN(FIND(MID(Id, 15, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 16 +
MIN(FIND(MID(Id, 14, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 8 +
MIN(FIND(MID(Id, 13, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 4 +
MIN(FIND(MID(Id, 12, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 2 +
MIN(FIND(MID(Id, 11, 1), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1) * 1 + 1,
1)
Is it an excel formula?
DeleteI am unable to use it.
This comment has been removed by the author.
ReplyDeleteYou can use CASESAFEID() in salsforce to generate a case insensitive id
ReplyDeleteI want to validate postal code using controller validation how do I do it?
ReplyDeleteI'm new to salesforce.
postal code: data type(Number(4,0))
I loved this content on saleforce's IDs. According to my opinion, Salesforce is an upcoming pioneer in the mainstream. Thanks to you.
ReplyDeleteaudience response system
audience response system
audience response system
audience response system
audience response system rental