3

I was just wondering what the best way to go would be in removing duplicates for two identical wrappers. Context: I have assigned contacts to a Contract. I have two tables in the Edit page:

  • Existing Contacts on Contract
  • Add new Contacts to Contract

I only want to list the contacts that are NOT assigned in the "New contacts" table.

I've tried a few things but my JAVA knowledge gap here is an issue. How would you guys go about this?

My idea was to run two For Loops - where it cycles through the first element of the Existing list, then runs a second For Loop IN that first loop, that cycles across the entire "All Contacts" list and deletes a matching value. I'm at a loss for coding this, however. Am I on the right track?

  //New method to Populate Contact List      

public void updateContactList(){


    //Initialize the list for exising contacts on Contract
    contactsExisting = new list<wrapContacts>();

    //populate a temoprary list of Contacts
    contact_to_contract__c [] varContactsExisting = [SELECT NAME,Contact__c, Contract__c, contact_Name__c, Role__c, Title__c FROM contact_to_contract__c where contract__c = :contractID];    

    for (contact_to_contract__C b: varContactsExisting){
        contactsExisting.add(new wrapcontacts(b));
        }    


    //Initialize the List (should be done every time this method is called only when loaded or change Acccount)
    contactsToSave = new list<wrapContacts>();

    //Populate a temporary list of Contacts
    Contact [] varContacts = [SELECT NAME, ID, Contact_Status__c, email, phone, contact_role__c  from Contact where AccountId =: contractObj.AccountId AND Contact_Status__c='Active'];

    //For each iteration in the list varContacts, add it to our Wrapper so we can display
    for (Contact a : varContacts){

         contactsToSave.add(new wrapContacts(a));


        }
    }    

Here's the wrapper info:

//Wrapper used for saving Contacts to Contacts to Contracts Object

public class wrapContacts{

    //Boolean to determine whether saved or not.
    public Boolean selectedContact {get;set;}   

    //Boolean to determine whether removed or not 
    public Boolean unSelectedContact {get;set;}

    //Contact to Contract object of Wrapper
    public Contact_to_Contract__c contactToContractObj {get;set;}

    //Contact object to find which ones to save.
    public Contact contactObj {get;set;}


    public wrapContacts (contact a){

        selectedContact = false;
        unSelectedContact = false;
        contactToContractObj = new Contact_to_contract__c();
        contactObj = a;
    }

    public wrapContacts (contact_to_Contract__c b){

        selectedContact = false;
        unSelectedContact = false;
        contactToContractObj = b;
        contactObj = new contact();
    }

}    

1 Answer 1

5

There's several approaches you can take. I would probably use a set:

public void updateContactList(){


    //Initialize the list for exising contacts on Contract
    contactsExisting = new list<wrapContacts>();

    //populate a temoprary list of Contacts
    contact_to_contract__c [] varContactsExisting = [SELECT NAME,Contact__c, Contract__c, contact_Name__c, Role__c, Title__c FROM contact_to_contract__c where contract__c = :contractID];    

    // Added this set here
    Set<Id> existingContactIds = new Set<Id>();

    for (contact_to_contract__C b: varContactsExisting){
        contactsExisting.add(new wrapcontacts(b));
        // Add contact Ids to the new set
        existingContactIds.add(b.contact__c);
        }    


    //Initialize the List (should be done every time this method is called only when loaded or change Acccount)
    contactsToSave = new list<wrapContacts>();

    //Populate a temporary list of Contacts
    // ** Added additional filter here **
    Contact [] varContacts = [SELECT NAME, ID, Contact_Status__c, email, phone, contact_role__c  from Contact where AccountId =: contractObj.AccountId AND Contact_Status__c='Active' AND Id NOT IN :existingContactIds];

    //For each iteration in the list varContacts, add it to our Wrapper so we can display
    for (Contact a : varContacts){

         contactsToSave.add(new wrapContacts(a));


        }
    }    
3
  • Oh wow, so you CAN use lists in SOQL queries. That's great! As for using a set: You used it because sets by default cannot have duplicate IDs in them, correct? Thank you for your help here, I'll try this out in the next hour! This helps greatly!
    – RC82
    Jun 15, 2015 at 19:58
  • This works like a charm! Thank you! More importantly, Sets seem to be fairly easy to use, and help solve such simple problems like this. I also learned how to say "select Ids that are NOT in this list/set", which is also incredibly useful. Thank you! I didn't know I could do that.
    – RC82
    Jun 15, 2015 at 20:57
  • @RC82 just remember that NOT IN by itself causes a full table scan (that's a Bad Idea), but using it in conjunction with a filter that can use indexes is perfectly okay... all lookup fields are indexed, so the account ID filter causes an indexed query.
    – sfdcfox
    Jun 15, 2015 at 21:13

You must log in to answer this question.

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