Any documentation or insight on the “empty id string”?

This answer (from Nathan Pike a Salesforce developer) mentions the idea of an “empty id string” that can be appended to the 3 digit key prefix. I’ve wanted an ID that matches nothing but is syntactically valid and is not null so am interested in official documentation or any further insight into this.

I note that all these compile and run as expected:

Id id = '003' + '000000000000AAA';
Contact[] cs = [select Name from Contact where Id = :id];
System.assertEquals(0, cs.size());
System.assertEquals(Contact.SObjectType, id.getSObjectType());

with the last line showing that using a real key prefix is safest.

PS

A Daniel Ballinger blog post on this – Salesforce Empty Key Id – 000000000000000AAA.

Answer

Empty Key Id In The Wild

I have only encountered the empty key Id in one circumstance. It represented the “All” List View in a peculiar circumstance I discovered a while back.

Mocking Your Own Dummy Id Values

In my experience, only the key prefix and length of the Id matter. Because of this, I can use the following logic to generate dummy Id values. Here is a simplified example of how I create dummy values in my SObjectFactory test utility.

public class DummyIdProvider
{
    final String prefix;
    Integer counter;
    public DummyIdProvider(SObjectType sObjectType)
    {
        prefix = sObjectType.getDescribe().getKeyPrefix();
        counter = 0;
    }
    public Object getValue()
    {
        return prefix + String.valueOf(counter++).leftPad(15).replace(' ', '0');
    }
}

Using Id values generated from this structure, I can easily mock trigger.oldMap, for instance.

final Integer recordCount = 10;
List<Contact> newRecords = SObjectFactory.create(
    Contact.ObjectType, recordCount, Contact.Id,
    new DummyIdProvider(Contact.sObjectType)
);
Map<Id, Contact> oldMap = new Map<Id, SObject>(newRecords.clone(/*preserveId*/ true));

I’m not sure what other circumstances necessitate dummy Id values. Perhaps there are some edge cases where the other components of an Id matter. See What are Salesforce ID’s composed of? for more detail, though @KeithC I’m sure you’ve read it and voted for it already. You still can’t update or query for records with a dummy Id, so I have a hard time believing the pod identifier is relevant.

Attribution
Source : Link , Question Author : Keith C , Answer Author : Community

Leave a Comment