0

I am trying to create a function which reads selected rows of contact and saves them only if they don't already exist. but i get error for "Before Insert or Upsert list must not have two identically equal elements" at the point of insert.

public pagereference import(){
        List<Related_Contact__c> selectedContacts = new List<Related_Contact__c>();
        List<Consultee__c> consultees = new List<Consultee__c>();
        for (consulteeWrapper cCon : wrapList){
            if(cCon.isSelect == true){
                Related_Contact__c cont = new Related_Contact__c();
                cont.Related_Person__c = cCon.statCon.Contact__c;
                cont.Role__c = cCon.statCon.Role__c;

                cont.Related_Activity__c = activity.Id;
                selectedContacts.add(cont);
                consultees.add(cCon.statCon);
            }
        }
        List<Related_Contact__c> uniqueContacts = new List<Related_Contact__c>();
        for(Related_Contact__c  existingCon: relatedConsulteeList){
            for(Related_Contact__c selectedCon : selectedContacts){
                if(selectedCon != existingCon){
                    uniqueContacts.add(selectedCon);
                }
            }
        }
        insert uniqueContacts;
        for(Consultee__c c : [SELECT Id, Process_Record_Type__c FROM Consultee__c WHERE Id =: consultees]){
            c.Process_Record_Type__c = processRecordType;
        }
        update consultees;
        selectedContacts = null;
        Pagereference page = new PageReference('/apex/ProcessView?id=' + statId);
        return page;
    }

2 Answers 2

1

I don't know how you're populating the existing list, so it may or may not have all the existing contacts in it, but your logic for checking dupes is incorrect:

for(Related_Contact__c  existingCon: relatedConsulteeList){
    for(Related_Contact__c selectedCon : selectedContacts){
        if(selectedCon != existingCon){
            uniqueContacts.add(selectedCon);
        }
    }
}

Basically what's happening is for the first existing contact, you're checking all of the newly selected ones, and if they don't match it, they're being added to the list. What happens with the rest of the existing contacts doesn't matter because you've already added them.

What you want to do is iterate over the selected contacts, checking each against the existing ones:

for(Related_Contact__c selectedCon : selectedContacts){
    boolean found = false;

    // check selected against all of the existing records
    for(Related_Contact__c  existingCon: relatedConsulteeList){        
        if(selectedCon == existingCon){
            found = true;
            break
        }
    }

    // if we failed to find a match then we'll insert
    if(!found)
        uniqueContacts.add(selectedCon);
}
0

Change the Type of uniqueContacts from a List<Related_Contact__c> to a Set<Related_Contact__c>. This will solve your problem by preventing it from containing two identical contacts.

2
  • thanks.. but then i cant insert a set. I get the error: DML requires SObject or SObject list type: Set<Related_Contact__c>
    – codeSlayer
    Jul 29, 2015 at 12:31
  • You can convert the set to a list to make the insert: insert new List<Related_Contact__c>(uniqueContacts);. See raydehler.com/cloud/clod/…
    – Rob
    Jul 29, 2015 at 13:48

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.