I have set up a customer community for a customer of ours that stores an Account ID on the User to determine what account the User views the community for. This account ID field has to be a text field since you can't create lookup fields on a User, so we are automating filling it in and then giving admins the ability to change it afterwards.

Above works, but I ran into an issue today where we had the 18 Digit ID stored on one user and the 15 digit ID on another. I constructed a set of the Account ID assigned to a user and the child account IDs of its children, and used a string query that used AND ID IN: accIDs. This worked for the 18 digit ID, but didn't for the 15 digit ID.

Does ayone know what the best practices/ places you should use the 18 digit ID vs. the 15 Digit one?

Here is a snippet of code-

set<string> nas = new set<string>();
for(integer i = 1; i <= 5; i++){
        List<Account> accs = [Select id, Name, ParentID From Account Where ParentID IN:nAs];
            FOR(Account a:accs){
//--Further Down--
String query = 'Select Name, ID, etc. ';
//More filters/fields, also Source__c is lookup to Account Object
query += ' AND Source__c IN: acids ';

Above shows an example of my code where I am generating a list of child accounts and then running the query with an 'IN:' filter to see if the ID is in the list of IDs.

  • 3
    The 18 and 15 digit Ids are treated as equivalent by all Salesforce APIs. Can you show the code where you're using this values? Did you receive an error, or just unexpected results?
    – David Reed
    Commented Aug 22, 2018 at 19:13
  • @DavidReed added. All I'm doing is constructing a list of IDs then using the IN: comparator in a string query to filter, which wouldn't work for the 15 digit ID but would for the 18 digit ID. Commented Aug 22, 2018 at 19:48
  • Whats the length of the stored ids in the text field? 15 or 18 characters? Salesforce is smart enough to match the ids, but not if the field isn't typed as an Id. Commented Aug 22, 2018 at 19:51

1 Answer 1


You should ideally never use the 15-character ID, especially in a text field where you're pretending to use it as a lookup. It becomes a lot harder to find the records you're looking for. When using real lookup fields, it doesn't matter, but for normal text fields, it does, because the matches are case-insensitive. Also, whenever you know you're dealing with an Id, do not use the String type: the Id type makes sure that you don't have invalid ID values and also restores the 18-character version of the ID if you load a 15-character String.

You must log in to answer this question.

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