Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
is there a documentation which says this? –  Dharshni Feb 7 at 12:11
add comment

2 Answers 2

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

share|improve this answer
    
/@sathya, do you know of a scenario where a duplicate EID would be needed/useful? –  Bartley Feb 7 at 12:48
2  
The problem with the unique attribute is that it can't be turned on if it would be violated by existing non-null values. This is particularly a problem with data that already exists in the database that is marked as "external id" sometime after its creation. In that case, you have to first fix up the values so they are unique before enabling the unique attribute. External ID fields should always be marked as unique, but for practical purposes, sometimes it can't be because of dirty data. –  sfdcfox Feb 7 at 18:06
add comment

Have you tried doing the upsert operation to see what it actually does when you don't mark it as unique? If this is an issue for you, when you create the externalId field you can set it to be a unique identifier and I'm pretty sure it behaves just like the Id field would when upserting.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.