function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick HanahanPatrick Hanahan 

How to find all duplicate Opportunities?

I'm trying to clean up my SalesForce a bit, and realized I have many Opportunities that are duplicates of each other. For me, duplicate Opps means multiple opportunities with the same "Job Number" which is a custom field. I tried to make it a unique field, but it won't allow me to do that until all duplicates are deleted.

So I'm trying to write some code that finds all duplicates at once and lists them so I can go through and delete the redundant ones.

I would like to just write a function that loops through all the Opps and checks like this. (pseudo code)
 
for ( Opportunity O: All Opportunities)
    {
        for ( Opportunity I: All Opportunities)
        {
            if O.jobNumber == I.jobNumber
                display I;
        }
    }
I just don't know if theres anywhere to plug that in. A report maybe? Is there anywhere I can access the entire database of opportunities and find specific ones in this manner?
 
Best Answer chosen by Patrick Hanahan
Sampath SuranjiSampath Suranji
Hi Patrick,
In the developer console (Setup> developer console) you can run below query
select id,name,job_Number__C from opportunity order by Job_number__C
Then you'll be able to see opportunities with the same job number.Then you can edit records or delete multiple records manually there.

Best regards
 

All Answers

Sampath SuranjiSampath Suranji
Hi Patrick,
In the developer console (Setup> developer console) you can run below query
select id,name,job_Number__C from opportunity order by Job_number__C
Then you'll be able to see opportunities with the same job number.Then you can edit records or delete multiple records manually there.

Best regards
 
This was selected as the best answer
Patrick HanahanPatrick Hanahan
Wow, thanks for the help Sampath. I didn't realize you could access the database directly with SQL statements like that. MUCH simpler than what I was trying to do. One quick question though, would it be possible to limit the query to ONLY opportunities that are duplicates? 

I tried adding "HAVING COUNT(Job__c) > 1" but it gave me an unknown error.

And again, thanks for the help.
Sampath SuranjiSampath Suranji
Hi Patrick,
From the below query we can identify the opportunities which are duplicated by using 'group by' clause. but the problem is the delete option is not working when we use group by clause
select  job_Number__C,count(name) from opportunity group by job_number__C
regards
.