How to debug the mysterious "duplicate value found: <unknown> duplicates value on record with id: <unknown>" error

My condolences if you're reading this, because it means that you're being tortured by an extremely useless and vague error message. Let's see if we can get through this without tearing out too much hair. :-)

Deployment Error or DML Error?

If you Google "duplicate value found: <unknown>...", the results you get will cover a wide variety of root causes. The first step is to figure out which scenario is appropriate to you.

DML Error

If you're getting this error from a DML operation, you're in luck. The possible solutions seem to be more straightforward. Sometimes, there might even only be one "<unknown>" instead of two to deal with.

Here are a few of the better posts for the DML-based errors:

  • Daniel Sokolowski's Blog - "Duplicate value found duplicates..."
  • Developer Forums - "Exception: duplicate value found: <unknown>..."
  • Developer Forums - "Duplicate value found: unknown duplicates value..."

Basically, it looks like the problem boils down to fields with unique constraints. Daniel's blog post does a great job of breaking down the debug process.

Deployment Error

If you're getting this error while trying to deploy metadata, things are a bit tougher.

Debugging deployment errors is almost always painful in some way, but this particular error message with its two vague "<unknown>" references is especially cruel. The following steps can help to quickly get you on the right track to finding a solution.

Step One: Identify the Problem

This step is easy to start, but sometimes difficult to finish. Take a look at the error message, and I'll explain what I mean.

Error: objects/Opportunity.object(2098,13):duplicate value found: <unknown> duplicates value on record with id: <unknown>

Clearly the Opportunity object metadata I'm trying to deploy is causing a problem. Don't start Googling yet, though. There's more for you to learn about what, exactly, is failing.

Forget about the error "message" for a moment, and focus on the error location.

Error: objects/Opportunity.object(2098,13)... 

The important bit here is (2098,13). This is the line and column number inside the Opportunity.object metadata file where the trouble is coming from.

Here's what my file looks like near that line...

Inspecting the Opportunity.object metadata file

This tells me that the error seems to be specifically related to the StageName picklist field that I'm trying to deploy.

Important: If this is EXACTLY the problem you're having, skip to the end to see a potential solution.

Step Two: Isolate the Problem

Deployments are tricky, complicated things with many dependencies to contend with. When debugging metadata errors that are not related to Apex code, simplify things by removing everything from your deployment package except the component that's giving you trouble (and its direct dependencies, of course).

If you're still getting the error after doing this, then you've successfully started the process of isolating the problem. In my case, I can try to Isolate things further by removing the entire StageName field and re-trying my deployment.

After removing that field, my deployment finally succeeds! This conclusively proves that something I'm pushing in the StageName field is causing the error.

Remember, this example is ONLY relevant to my situation. The metadata components that are giving you trouble will most likely have different names, be different types, etc. This is just an example.

Step Three: Start Digging

Now that I've conclusively isolated the problem to a particular field on a specific object, I can start digging deeper. Here's what I've found to be the most common possible causes.

For Picklist Fields...

If you've narrowed things down to a problem with a Picklist field, make sure that your target org does not already have an inactive picklist field with the same name as one of the ones you are trying to deploy.

If this is the case, you'll need to delete the picklist value. Once you do this, your deployment should proceed (until the next error sneaks up on you, of course).

For Other Problems...

If your problem is not related to a picklist field, the general consensus seems to be that there are some strange situations that come up if Field History Tracking is enabled on the object that is giving you trouble.

Here are the best solutions I've found related to "Field History Tracking" being a possible issue.

  • DaveS (StackExchange) - "Object Deploy fails with: duplicate value found: <unknown>..."
  • Michael Welburn's Blog - "Salesforce.com Metadata API Error: Duplicates Value on Record with ID"
  • Known Issues In Review - "Deployment fails with duplicate value found: <unknown>..."

Conclusion

Debugging Salesforce errors is never fun. It's especially not fun when the error messages are vague, like the unhelpful "duplicate value found: <unknown>..." error message. This post has provided a framework for debugging this type of error, whether it came from a DML or a metadata deployment operation.

The steps from the "Deployment Error" section are useful for debugging other types of non-code deployment related errors. Hopefully you've found them useful. As time goes by, if additional "flavors" of this error show up, I'll update this answer to include them. Good luck out there!


Known issue : Deployment fails with duplicate value found: duplicates value on record with id: error

I hope this might help : If you are facing this error for page layouts, here is the best workaround !

Workaround Solution :

  1. Quick Actions for either salesforce classic or lightning, buttons on related lists may be showing duplicates, which should not be. (Observe the entire page layout for duplicates)
  2. Removing them and saving from the source environment doesn't work (duplicates re appear after saving).
  3. Retrieve via ant and edit retrieved page layout file to eliminate duplicates manually.
  4. Deploy the retrieved & modified file from your local to Target environment.

    Known issue : Deployment fails with duplicate value found


After many hours trying to look for every single issue mentioned on this page, and getting so angry about the lack of useful error messages in DX I was reminded that deployment settings may give more info.

The error I was getting in DX was just N/A duplicate value found: <unknown> duplicates value on record with id: <unknown>. The Path was just showing as N/A.

However, when I go into Deployment Settings in the org, the API Name is shown.

The Error in Deployment Settings - shows me the duplicate Now, I think I know why the Path showed N/A in this instance, because the source was stuffed up due to a report being duplicated. I had created the report in Public Folders, pulled the source, then moved the report to an existing folder, and pulled the source again. See the Issue 165 in the CLI Github and the related issue. I was able to use the workaround mentioned to fix my source and then deploy.

I hope this trick of trying to find the error helps someone else.