You need to sign in to do that
Don't have an account?
kevincar64
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.
try:
Then you can build a list of IDs that have that email address or however you need to do it
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.
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?
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:
... 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
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
[SELECT ID, Email from Lead where Email IN (SELECT Email, count(Id) from Lead group by Email HAVING count(Id) > 1)]