1

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-

acids.add(accSel);
set<string> nas = new set<string>();
nas.add(accSel);
for(integer i = 1; i <= 5; i++){
    IF(!nas.isEmpty()){
        List<Account> accs = [Select id, Name, ParentID From Account Where ParentID IN:nAs];
        IF(!accs.isEmpty()){
            nAs.clear();
            FOR(Account a:accs){
                accounts.add(a);
                nas.add(a.id);
                acids.add(a.id);
            }             
        }
    }
}
//--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
  • 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 Aug 22 '18 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. – Josh Schoenbachler Aug 22 '18 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. – battery.cord Aug 22 '18 at 19:51
3

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.

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.