8

I've been searching the documentation for this, without much luck so far. In particular I'd like to convert an Id to a 18-char string, and ideally read about that in documentation to verify its the recommended approach.

I've seen various approaches including

String str = '' + someId;

Is this the same as

String str = (String)someId; 

I've also seen

String str = String.valueOf(someId);

However when I tried that I got 15-char strings! Why is that?!

What is the recommended approach to always guarantee an 18-char string from an Id?

9

You need to get it as Id.valueOf('someId'). This will return the 18 char id. See below:

String strId = Id.valueOf('yourId');

This will store the 18 char Id in String format.

| improve this answer | |
  • I don't want an Id result, I want an 18-char string as the result – Brad Thomas Dec 11 '14 at 19:46
  • 1
    It will work with strings.. updated the answer. – Brad Bumbalough Dec 11 '14 at 19:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.