1

I have an account record named with sForceTest1.

The sForceTest1 account have dup1,dup2,dup3,dup1,dup2 and dup3 contacts, By seeing those contacts, you can known that sForceTest1 have duplicate contact records.

So far I tried with the following snippet.

Public class Example11 {
    Public List<Account> accs {set;get;}
    Public Set<String> setresult {set;get;}
    Public List<Id> conids {set;get;}

    Public void main(){
        setresult = new Set<String>();
        conIds = new List<Id>();
        accs = [select id,name,(select id,lastname,AccountId from contacts) from Account where id='0012800001EcUSl'];
        for(Account aa : accs) {
            for(Contact cc: aa.contacts) {
                setresult.add(cc.lastname);
                conids.add(cc.id);
            }
        }
    }
}

I think above code is not useful to get the ids. By using this code I can get the names where as i couldn't get the ids of those records.

How can I get the ids of these duplicates records?

  • dup1,dup2,dup3,dup1,dup2 and dup3 contacts you mean they have lastName equals dup1,dup2,dup3,dup1,dup2 and dup3 ? – benahm Mar 28 '17 at 14:16
  • I think that this code is supposed to work. Your ids are supposed to be in the conids list. Why are you saying that this doesn't work? Is this list empty? – Martin Lezer Mar 28 '17 at 14:17
  • @beham, yes. Those were created with lastname. like LastName = dup1 – KS Kumaar Mar 28 '17 at 14:18
  • @MartinLezer, This code will give all the ids including duplicate names also. Actually i need ids of duplicate records. – KS Kumaar Mar 28 '17 at 14:20
  • Are you trying to get last name wise duplicate ids? like map of name and duplicate ids? – Safiya PV Mar 29 '17 at 9:12
1

Your code will work perfectly with slight modification. The below code checks if set contains contact lastname. If its true the current contact is a duplicate. So you can add it to conids. Else add lastname to the set. Now conids will have all duplicate contact ids of particular account.

Public class Example11 {
    Public List<Account> accs {set;get;}
    Public Set<String> setresult {set;get;}
    Public List<Id> conids {set;get;}

    Public void main(){
        setresult = new Set<String>();
        conIds = new List<Id>();
        accs = [SELECT id, name, 
                       (SELECT id, lastname, AccountId 
                        FROM contacts) 
                FROM Account 
                WHERE id = '0012800001EcUSl'];
        for(Account aa : accs) {
            for(Contact cc: aa.contacts) {
                if(setresult.contains(cc.lastname)){ //Checks if set contains contact lastname.
                    conids.add(cc.id);
                }
                else{ //If not contains add lastname to set
                    setresult.add(cc.lastname);
                }
            }
        }
    }
}
|improve this answer|||||
  • The above code is working for records if we have duplicates more than only one. And its doesn't work for records if we have duplicates more than two 2 (like dup1,dup2,dup3,dup1,dup2,dup3,dup1,dup2 and dup3) – KS Kumaar Mar 29 '17 at 7:38
  • @KSKumaar I didn't get what do you mean by duplicates more than any number. If dup1 is a contact any contacts having last name as dup1 is a duplicate. So in that case the above code will work. In your question you need duplicate contact ids which will be there in conIds – Reshma Mar 29 '17 at 8:07
  • Sorry for the delay. I Just small changes in what you suggested. Finally i got my requirement. Thank you @Reshma – KS Kumaar Mar 29 '17 at 9:17
  • 1
    @KSKumaar glad to help you. – Reshma Mar 29 '17 at 9:37
1

This query gets you a list (aggregate) of lastNames of dupContacts for a given account Id

List<AggregateResult> aggrDupContacts=[select LastName from contact where AccountId=:accId group by LastName having count(Id)>1];

parse the List<AggregateResult> to a List<String>

Then you can get the list of dupContact ids with a simple SOQL query

List<Contact> dupContactIds=[select id from contact where lastName in :dupContactNames]

Public class Example11 {
    Public List<Contact> conts{set;get;}
    Public Set<String> setresult {set;get;}
    Public List<Id> conids {set;get;}

    Public void main(){
        setresult = new Set<String>();
        conIds = new List<Id>();
        conts = [select id,lastname,AccountId from Contact where Accountid='0012800001EcUSl' order by LastName];
        for(Contact cc: conts) {
             if(setresult.contains(cc.lastName)){
                 conids.add(cc.id); 
             }

             setresult.add(cc.lastname);
        }
    }
}
|improve this answer|||||
  • I will try this and let you know. – KS Kumaar Mar 28 '17 at 14:57
  • As i expected this line List<Id> dupContactIds [select id from contact where lastName in :dupContactNames] again will return all ids including duplicates. Even i checked too for verification. – KS Kumaar Mar 29 '17 at 6:11
  • @KSKumaar Yeah, I see, but when you have 2 contacts with LastName=DUP you can't tell if the dup is the first or the second one, to achieve that you need to have a criteria that tell you which is the dup and which is not, otherwise you have to do that randomly in Apex – benahm Mar 29 '17 at 7:35
  • Yeah @benahm, i am trying it in multiple ways. Reshma answer working in one conditions and have to modify it. – KS Kumaar Mar 29 '17 at 7:40
  • @KSKumaar try the code above, I followed your code... – benahm Mar 29 '17 at 8:23
1

Try this here you get last name wise duplicate ids.

Map<String,set<id>> contMap = new Map<String,set<id>>();
for(contact c:[select id,lastname,AccountId from Contact where AccountId = '0012800001EcUSl']) {
    if(contMap.containsKey(c.lastName)){
        contmap.get(c.lastname).add(c.id);
    } else
        contMap.put(c.lastName, new Set<id>{c.id}); 
}
System.debug(contMap);
for(string key:contMap.keySet())
   if(contMap.get(key).size() == 1)
       contMap.remove(key);
System.debug(contMap);
|improve this answer|||||

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.