Deployment Error duplicate value found when building unique index

When deploying Custom Fields with Copado you might encounter the following deployment error:

DUPLICATE_VALUE: duplicate value found when building unique index: XXXX on rows (XXXX) and (XXX)

This error is returned by Salesforce.

Whenever a field is set to Unique, Salesforce will index the field. During the process, if duplicate values are found on the records, you will face this error.

If you have a field in both source and target environments and you make it Unique, Salesforce will try to index the field in target organization when deploying and the error will be returned if the target org contains records with duplicate values on that field.

The error can be reproduced easily in the Salesforce UI if you set the same field to Unique in the target organization.

How did we do?