After doing an import, there seems to be duplicate records for one of our custom objects. There are too many records to do that manually.

Is it possible to write apex code that removes duplicate records?

e.g. (Kind of psuedo-code; because i don't know apex that well)

Items item =   [SELECT id from item where id=:id]
delete item;
  • I think Google might be able to help you, a quick search I pulled up this.. salesforce.stackexchange.com/questions/17068/… – EricSSH Mar 23 '15 at 16:48
  • I'd probably just identify when the records were imported and delete all records that were created in that time frame (as long as it was suitably narrow) – Caspar Harmer Mar 23 '15 at 17:57

Of course it is possible to do with Apex. And, there are a variety of free and paid options for 3rd party tools that can de-duplicate or block creation of duplicate records (Salesforce Duplicate management, DupeCatcher, Dupeblocker/DemandTools, etc.). It really depends on how you can identify the duplicates in the first place. For the declarative configuration route, if there was one field that would identify duplicates (like some type of external ID, you could set that ID field as unique in the setup, preventing the duplicates from loading. For the Apex option, you could use a trigger to scan for potential duplicates based on your rules for identifying them, and either, block them from creation, or flag as potential duplicates for manual review, depending on your business requirements. Lastly, you could develop a scheduled batch job that would analyze the object for potential duplicates.

First step for you is to determine how to identify a duplicate, and what do do if a duplicate is being saved in the system. THen, you can move on to the "how" to detect and handle.


There is not a magic answer unfortunately. If this is a one and done situation I would either power through this one or pull the records back with data loader (delete) and work with Excel for a better data set.

If this is going to be an ongoing situation I would invest in Demand Tools as it makes quick work of merging and updating large data sets of data. Again, you need to know the tool and the data but it will save you a lot of time in trying to create an apex solution or doing a bunch of excel vloolups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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