.ultrageek. Computers, Music, and Thoughts

18Sep/190

Silly Dev Trick: Converting Ids from 15 to 18 Automagically in Apex

Anyone that has dealt with Opportunity Line Items knows that you cannot create a lookup to that record on another. So the next best thing is to put a text field and store the Id. Apex stores Ids in their 18 length form but in the UI it shows it to you as 15 digits. Since it is not uncommon for folks to update data manually I needed a quick way to 'fix' Ids from 15 to 18 in apex like CASESAFEID. Workflow would have worked...but let's pretend it didn't heh.

Here's a quick trick to convert those 15 digit text ids back to 18:

string idStr = '001E000000nwg7g';   // 15 character id
id idval = idStr;                   // assign to ID variable
idStr = idval;                      // back to string to prove the point
system.debug('18 char: ' + idStr);  // note that you could just append idval instead
                                    // of converting to string first

Got this trick from this awesome Stackexchange post: https://salesforce.stackexchange.com/questions/25575/casesafeid-apex-equivalent

As usual this was a little harder to find than it should have been.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.