I recently found that some strings, which are very much NOT Salesforce Ids, will cause instanceof Id to return a false positive.


String extId = '243209199570387970';
Boolean isId = extId instanceof Id;
System.debug(isId); // ouputs TRUE but should be false

Is this just simply a bug or is there some weird intended behavior in this? Are there any alternatives to checking if a String is a Salesforce ID?

  • worth noting that I mock Ids all the time using 001000000000000 or 001000000000001 for different accountIds when unit testing methods that accept sobjects as args
    – cropredy
    Dec 20, 2022 at 19:20

1 Answer 1


Their check on instance of Id just checks if it is instance of String and the length of the String is either 15 or 18 characters. It also allows to type in any symbol that cannot be used in ID, such as ;, for example. It is not a bug, but rather not expected behavior, as you can run this code without errors, too:

Id extId = '243209199570387970';

As a workaround you can try this:

String extId = '243209199570387970';
Boolean isId = true;
try {
    ((Id) extId).getSobjectType();
} catch (Exception e) {
    isId = false;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .