Subscribe to my full feed.

This is Part 3 of 5 in my series on converting 15-character Salesforce IDs seen in the Salesforce UI into the 18-character IDs seen in results from the Data Loader and API calls. You can start at Part 1 here.

In Part 2, we walked through the algorithm for converting the ID. Now Part 3 will cover the basic pieces of doing it in Apex. (code improvement suggestions welcome!)

Block 1: Convert to Binary
1. Take each letter in a 15-character string
2. Convert it to binary (1 for capital letter, 0 for anything else)
3. And put it back into a 15-character string (backwards, as required)



Block 2: Function for converting 5-character binary strings
1. Create a big ugly map (binary chunks to a letter)
2. Use the chunk you passed in to get a letter from the map
3. Return the letter



Block 3: Build the result string
1. Start with the original 15-character string
2. Convert the binary string 5 characters at a time (with the above function)
3. Append the resulting letter to your final string

And that's it! Following the algorithm published by Salesforce (see Part 2 for details), we converted a Salesforce ID into binary, reversed it, and used 5-character chunks against a map to get the final 3 letters of the new, 18 character ID.

In Part 4 we will put this Apex into a Visualforce controller, along with a simple page.

0 comments:

Post a Comment