Search code examples
salesforceapexsalesforce-lightning

Adding a Unique Index When Duplicate Values Exist in the Column


I have a problem with creating a unique existing field. Standard Object - Account; Field - EMail (several instances of the Account object with the same mail have already been created). I want to make the field unique (do not repeat the values), how can I solve the problem with the existing data correctly? If through the method "point and click" to act, then an error is generated:

Error: Duplicate value (s) found when building unique index, example: blabla @ gmail.com on rows


Solution

  • You'll have to delete the records (or maybe just clear the email fields on some?) before you can apply unique index. Salesforce has duplicate rules, you could use them to find matches, maybe the answer is to use merge operation, not delete? You can merge manually or with Apex. Or just run a report on Accounts grouped by email, sort by count descending and see what needs fixing.

    If that bad data has to stay as is - best would be to configure duplicate rules to look at the field and prevent creating new? In this trailhead 2nd module has some screenshots that show how you can configure it to BLOCK inserts & updates. Not as good as true unique field but it's something...

    Worst case you could cheat a bit. I mean depending on address if it's gmail then googlemail.com works, putting dots in the part before @... Won't work with all addresses though.