What is the use of External ID field, not unique?

What can be the use of an external ID field, not marked as unique? Helps in indexing?

I checked the documentation , and found that to upsert records with an external ID, there is no requirement that the external ID be unique.
So, if I upsert records using a non-unique external ID, will I not get error saying “Duplicate External IDs” found? If not, how will the upsert pick records that have duplicate values in this external ID field?

Thanks.

Answer

Unfortunately you are right, using external IDs can lead to multiple matches during upsert operations.. taken from the documentation on Upserting examples:

If the key is matched multiple times, then an error is generated and the object record is neither inserted or updated.

So in answer to your question, yes you will get an error! Frustrating as it is.

Edit: This would need checking, but there is a specific exception type DmlException, which I imagine is what would be thrown, and you could (and probably should anyway) look to catch after an upsert call

try
{
    upsert yourList;
}
catch (DmlException e)
{
    // Report your error and/or try to re-handle the list?
}

Edit 2(!): Don’t forget of course as well you can set your external ID to have unique (and case sensitive, and required) properties, which I would personally recommend if you ever intend on using the field in upsert commands.

Image

Attribution
Source : Link , Question Author : Dharshni , Answer Author : Sathya

Leave a Comment