5

I have a condition to check the id stored in a custom object; it should match with the user profile id in Apex.

CustomObject.ID_Value__c = '00ea0000001p22S'; //I got this from profile detail page url 

Profile p = [select id from profile where name = 'General user Profile' limit 1];

System.debug(CustomObject.ID_Value__c); // 00ea0000001p22S
System.debug(p.ID) // 00ea0000001p22SAAQ extra digits added

So how got the condition mismatched? How to overcome this?

11

They're the same.

system.assertEquals(Id.valueOf('00ea0000001p22SAAQ'), Id.valueOf('00ea0000001p22S'));

Salesforce Ids have a 15 digit Id and an 18 digit Id. The longer version is "case-safe" in order to support integrations with a wider array of external systems. For more detail around the conversion from 15 digits to 18 digits, see What are Salesforce ID's composed of?

5

While Adrian's answer is entirely correct that these two values will evaluate as equal as long as at least one of them is an id, I wanted to also point out that in your example, CustomObject.ID_Value__c is being displayed in the debug logs without the three-letter suffix because the ID_Value__c field is a text field and not a lookup. Consider the following:

Account a = new Account();
a.Id = '0019000000PElZu';
a.AccountNumber = '0019000000PElZu';
system.debug(a.Id);                                     // 0019000000PElZuAAL
system.debug(a.AccountNumber);                          // 0019000000PElZu
system.debug(a.Id == a.AccountNumber);                  // true
system.debug(String.valueOf(a.Id) == a.AccountNumber);  // false

This happens because, link,

If you set ID to a 15-character value, Apex converts the value to its 18-character representation. All invalid ID values are rejected with a runtime exception.

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.