20
3
In Salesforce CRM, every object has 15-character alphanumeric ID, which is case-sensitive. If anyone's curious, actually it's base-62 number. However, tools used for data migration and integration may or may not support case sensitivity. To overcome that, IDs can be safely converted to 18-character case-insensitive alphanumeric IDs. In that process 3-character alphanumeric checksum is appended to the ID. The conversion algorithm is:
Example:
a0RE000000IJmcN
Split ID into three 5-character chunks.
a0RE0 00000 IJmcN
Reverse each chunk.
0ER0a 00000 NcmJI
Replace each character in every chunk by
1
if it's uppercase or by0
if otherwise.01100 00000 10011
For each 5-digit binary number
i
, get character at positioni
in concatenation of uppercase alphabet and digits 0-5 (ABCDEFGHIJKLMNOPQRSTUVWXYZ012345
).00000 -> A, 00001 -> B, 00010 -> C, ..., 11010 -> Z, 11011 -> 0, ..., 11111 -> 5`
Yielding:
M A T
Append these characters, the checksum, to the original ID.
Output:
a0RE000000IJmcNMAT
Write program or function which takes 15-character alphanumeric (ASCII) string as input and returns 18-character ID.
Input validation is out of scope of this question. Programs may return any value or crash on invalid input.
Please, don't use Salesforce propretiary languages' features that make this challenge trivial (such as formula CASESAFEID()
, converting Id
to String
in APEX &c).
Test Cases
a01M00000062mPg -> a01M00000062mPgIAI
001M000000qfPyS -> 001M000000qfPySIAU
a0FE000000D6r3F -> a0FE000000D6r3FMAR
0F9E000000092w2 -> 0F9E000000092w2KAA
aaaaaaaaaaaaaaa -> aaaaaaaaaaaaaaaAAA
AbCdEfGhIjKlMnO -> AbCdEfGhIjKlMnOVKV
aBcDEfgHIJKLMNO -> aBcDEfgHIJKLMNO025
3Sadly, converting a string to an Id in Apex Code still wouldn't be shorter than some of the answers provided here, especially if the code must be self-contained. Apex Code is not well-suited for golfing. – phyrfox – 2016-01-12T06:34:10.173
2@phyrfox as a former salesforce dev. Apex isn't suited for much... – Mike McMahon – 2016-01-12T09:08:30.760
2APEX, 56 bytes:
public class X{public X(Id i){System.debug((String)i);}}
. Works only with valid Salesforce IDs, though. – Trang Oul – 2016-01-12T11:49:45.693I came here looking to actually do this for work (https://success.jitterbit.com/display/DOC/Formula+Builder+String+Function), not golf, but I'm a little confused by the description of the algorithm. You say each reversed-and-sanitized chunk in step 4 will be a "binary number," but you never replace digits 2-8 with 0's and 1's. What exactly am I supposed to do for step 4 when steps 1-3 on a chunk like "62mPg" have resulted in a number like "01026"?
– k.. – 2017-10-27T18:04:37.157