I want to transfer the contacts from account AA to account BB without duplicates.

If Account AA have Dup2,Dup3,Dup4,Dup2,Dup3,Dup4,Dup2,Dup3 and Dup4 contacts, for the first time We need to transfer Dup2,Dup3 and Dup4 to Account BB. If we run second time, it should not transfer duplicate records again and keeping them in same account AA.

Public class Example11 {
    Public List<Account> accs;
    Public List<Account> aacc;
    Public List<Contact> conupdate;
    Public Set<Contact> setUpdate;
    Public Set<String> setresult;
    Public List<Id> conids;

    Public void main(){
        setresult = new Set<String>();
        conIds = new List<Id>();
        setUpdate = new Set<Contact>();

        accs = [select id,name,(select id,lastname,AccountId from contacts) from Account where id='0012800001EcUSl'];
        for(Account aa : accs) {
            for(Contact cc: aa.contacts) {

        List<Contact> consd=[SELECT id,lastname,AccountId FROM Contact WHERE Id=: conids];
        aacc = [select id,name,(select id,lastname,AccountId from contacts) from Account where id='0012800001EcUSj'];
        for(Account aa : aacc) {
            for(Contact conc: aa.contacts) {
                for(Contact cca : consd) {
                    if(conc.lastname != cca.lastname){
                        cca.AccountId = '0012800001EcUSj';
        conUpdate = new List<Contact>(setUpdate);
        update conUpdate;

I tried with above class. For the first time, i can able to send Dup2,Dup3 and Dup4 to Account BB with out duplicates.

But the first issue is, while i am checking second time, duplicates contacts are saving to Account BB.

Second issue is, Contact records can able to transfer if account BB have atleast one contact. If account BB does not have any contacts, Dup2,Dup3 and Dup4 are not transferring.

I tried with lot of efforts without success. But i couldn't succeed it.


1 Answer 1


you need to change the account id of contacts after checking in new contact list so change account id outside the for loop.

for(Account aa : aacc) {
   for(Contact conc: consd) {
       Boolean found = false;
       for(Contact cca : aa.contacts) {
           if(conc.lastname == cca.lastname){
                found = true;
        if (!found) {
            conc.AccountId = 'your accountid' ;

there are many nested loops so you can avoid them by using map. The above code works same as your code with solution to your problem.

Map<String,id> contMap = new Map<String,id>();
for(contact c:[select id,lastname,AccountId from Contact where AccountId = '0016F00001ya0sO']) {


if(contmap.size() > 0) {
    Map<id,Contact> consd=new Map<id,Contact>([SELECT id,lastname,AccountId FROM Contact WHERE Id=: contMap.values()]);
    List<Account> aacc = [select id,name,(select id,lastname,AccountId from contacts) from Account where id='0016F00001wiKsL'];
    Account acc2 = new Account();
    acc2 = aacc.isEmpty() ? acc2 : aacc[0];
    if(acc2.id != null) {
        Map<String,Contact> newCont = new Map<String,Contact>();
        for(Contact c : acc2.contacts) {
            newCont.put(c.lastname, c);


        List<Contact> setUpdate = new List<Contact>();

        for(String key: contMap.keySet()) {
            if(!newCont.containsKey(key)) {
                Id contId = contMap.get(key);
                System.debug(contId+' '+consd.get(contId));
                consd.get(contId).AccountId = acc2.id;
        update setUpdate;

  • Yeah blackPerl. This code some what working. I need to change this and let you inform tomorrow.
    – KS Kumaar
    Mar 30, 2017 at 15:03
  • and if you observed i changed the order of looping
    – Safiya PV
    Mar 30, 2017 at 15:04
  • It did not work?
    – Safiya PV
    Mar 31, 2017 at 6:02
  • what is the issue? change the accpountids. those are my accountids.
    – Safiya PV
    Mar 31, 2017 at 6:04
  • Let us continue this discussion in chat.
    – KS Kumaar
    Mar 31, 2017 at 6:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .