1

I have the following code:

String m = '{"Id": "a186D000000EYJ2"}';

Reference__c reff = (Reference__c)JSON.deserialize(m, Reference__c.class);  

System.debug(reff);
System.debug(reff.Id);

The output is:

16:52:14:005 USER_DEBUG [5]|DEBUG|Reference__c:{Id=a186D000000EYJ2}

16:52:14:005 USER_DEBUG [6]|DEBUG|a186D000000EYJ2QAO

Note that the printed ID is different. What is the cause of this?

1
  • 2
    3 extra characters are checksum of Id, both versions, short nad long, should work properly for you. – patryk May 15 '20 at 7:04
5

Salesforce Ids are both 15 and 18 character long. So for salesforce those two ids are the same. (notice that the only difference is the last 3 capital-case characters).

To simplify things,

  • the 15 character Id is uniquely case-sensitive
  • the 18 character Id is uniquely case-insensitive (you can calculate this one based on the 15 chars one)

You could say that a Salesforce id has 3 parts

  • [3] sObject prefix
  • [12] sObject-based case-sensitive Id
  • [3] ("optional") calculation that makes the full Id case insensitive

When you use a variable of type Id, salesforce is using the 18 character one (even if you assign the 15 one, salesforce calculates the other 3 characters automatically)

Why do you need a 18 character Id if the 15 is already unique in a case-sensitive way? Well... there are many tools where you can't use the "case", for example VLOOKUP in excel (even salesforce itself has some automatic case insensitivity builted in).

Notice that there are some automations in place in Salesforce regarding this kind of field-types, so for example, if you have in the where-clause of a query a field of type Id (Id, Look-up, etc) and compare it against a string of 15 characters or 18 characters, the query would still return the good values)

Some useful links:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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