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
kevincar64kevincar64 

SOQL Query Help

I'm in a little bit of a jam.

I need to find all the IDs of Leads that have duplicate emails.

In SQL it would be something like

 

SELECT ID, Email from Lead where Email IN (SELECT Email from Lead HAVING COUNT(Id) > 1)

 

Does anyone know ho I can do this?

 

Many thanks for any suggestions.

 

 

Starz26Starz26

try:

 

AggregateResult ar = [SELECT Count(ID)cnt, Email from Lead GroupBy Email];


 

Then you can build a list of IDs that have that email address or however you need to do it

 

 

kevindotcarkevindotcar

Hi Starz,

 

Sorry, I wasn't clear.

I also need the Id of the repeated records, because I need to reassign all of them (all leads that have a duplicate email) to a "trouble queue" -   so that a lead sales analyst can see what's going in with them.

 

I have potentially thousands of leads with duplicate emails, so I can't use your query as an inteermediate query, because I'll hit governor limits.

 

 

carlocarlo

Cant you build a list of the Emails which are repated using the above aggregate result and the run another query for all records where Email in :list?

kevincar64kevincar64

Hi Carlo,

 

It's thousands of leads I have to reassign to a queue. I'm afraid of running up against governor limits if I

have nested SOQL queries.

 

What I'm thinking of doing is just running a SOQL qry that sorts all the leads by email into a list, like this;

   List<Lead> l = [SELECT Id, Email, CreateDate WHER CreateDate = TODAY ORDER BY Email ASC];

 

...and then have a nested for-loop to iterate from top to bottom thru the list to see which emails are duplicated

from the outermost loop, and re-assign the duped emails to the "trouble" queue.

 

I was hoping to get awayfrom this because it seems like a hack - I'd rather have an elegant SOQL query

that is constrained to the "troubled" (ie; duplicated emails) leads.

 

Just to give you an idea of what I'm doing procedurally I'm looking at something like this:

 

   List<Queue> tqscope  = [SELECT Id, Name FROM Group Where Name = 'Trouble Queue'];
   Id dleadq  = tqscope[0].Id;
   List<Lead> emscope  = [SELECT Email, CreatedDate, 
 		FROM Lead
		WHERE Email != null
		AND OwnerId != :dleadq
		AND  CreatedDate <  2012-03-14T00:00:00.000Z
		ORDER BY Email ASC];    // TEST IN SANDBOX

     for(integer iidx =0; iidx < emscope.size() - 1; iidx++){
         Lead l = emscope[iidx];
         for(integer jjdx =; iidx+1 < emscope.size(); jjdx++){
	     integer modifyct = 0;
             //re-assign the jjdx lead's owner becaus its duped
             if(emscope[jjdx].Email == emscope[iidx].Email) {  
               modifyct++;
               emscope[jjdx].OwnerId = dleadq ;
               emscope[jjdx].Debug__c = 'debug_queued_d';
               updLeads.add(emscope[jjdx]);
               }
            else {
		if(modifyct > 0) {
  		   emscope[iidx].OwnerId = dleadq;
		   emscope[iidx].Debug__c = 'debug_queued_d';
		   updLeads.add(emscope[iidx]);
		   }
	       break;
	       }
            }
      update updLeads;   // the list of stuff to update
      }

 

 ... As you can tell, it looks more like C or FORTRAN that any kind of well-written Apex.  I haven't tested it yet so it's not real - I'm just trying to illustrate in code what I'm trying to clean up with a better-cinstrained SOQL query. 

 

Thanks for any input, and I hope this kind of clarifies.  I really appreciate the input from the community.

 

KC

 

 

 

carlocarlo

Big guess - ???

 

 

[SELECT ID, Email from Lead where Email IN (SELECT Email, count(name) c from Lead HAVING c > 1)]

 

You need to try running the sub query - (SELECT Email, count(name) c from Lead HAVING c > 1) and looking at the results

carlocarlo

[SELECT ID, Email from Lead where Email IN (SELECT Email, count(Id) from Lead group by Email HAVING count(Id) > 1)]