Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any function in Apex which could emulate functionality of the CASESAFEID() formula function? I'm trying to get 18-char Id variant from the standard one.

share|improve this question
up vote 20 down vote accepted

Per the Apex docs on primitive datatypes, "Note that if you set [an] ID to a 15-character value, Apex automatically converts the value to its 18-character representation." So it should be enough to assign your 15 character id to an ID-typed variable, and then back to a string, like so:

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

Remember that assigning an invalid id string to an ID-typed variable will throw an exception; if there's any chance that your 15 character value may not be a valid id, wrap the assignment in a try/catch block.

share|improve this answer
Good enough for me :) Thank you, Jason! – dino ursic Jan 30 '14 at 14:06

You can just type the string cast to ID. Like (Id) idStr;

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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