I have some code like this. While the conditionals I have are working fine what I cannot check for is if Contact lCon is NOT contained in the sfdcList. I have been trying to find a way to set up a Set or List and contains() method to do this but it all doesn't seem to work as I expect.

lookerByIds is a Map<Id, List<Contact>> So the intent here is that each element of the sfdcList for a given Id is checked against a list of contacts by Id. See if they have conditionals like so and if the lCon list is in the sfdcList at all. Then do something else.

for(Contact con : sfdcList){

            //specifies the same key value pair as the queried Contact
            for(Contact lCon : lookerByIds.get(con.AccountId)) {

                if(con.Email !=Null && lCon.Email !=Null) {
                    //compares email values 
                    if(!con.Email.equalsIgnoreCase(lCon.Email)) {
                    if(con.Email.equalsIgnoreCase(lCon.Email)) {
                        //adjusts record
                        if(con.status__c == Null)
                            con.status__c = 'Billing Contact';

                        if(con.status__c != Null && !con.status__c.contains('Billing Contact')) {
                            con.status__c = con.status__c + ';Billing Contact';
                        if(con.LastName.equals('Accounts Payable') && con.status__c.contains('Billing Contact')) {
                    else {

                    Contact bub = new Contact(LastName = 'Accounts Payable', AccountId = lCon.AccountId, status__c = 'Billing Contact', Email = lCon.Email);
  • you can put if check to match the Id, inside second for loop. if(con.Id != lCon.Id) – Sarvesh Sep 13 '19 at 17:57
  • In this case they all share the same Id (Account ID), I can only compare emails. lCon is from an external data source that only had email addresses and SFDC Account Ids – Randy B. Sep 13 '19 at 17:59
  • As I understood from your question like you want to check if lCon is contains in sfdcList or vice versa. That's why I tell to compare the lCon.Id with con.Id. – Sarvesh Sep 13 '19 at 18:04
  • Yes but lCon is a List<Contact> of a map keyed by the same Id as SfdcList AccountID, What I need to do is see if lCon email address exists in the sfdcList – Randy B. Sep 13 '19 at 18:06
  • Okay, how lookerByIds map form, is this data come from the lighting component. – Sarvesh Sep 13 '19 at 18:14

The easiest way to test for which elements in set 1 are not in set 0 is as follows:


After the operation, set1 will only have elements not in set0. hence it is a destructive operation so you may choose to do a deepclone of the set before the removeAll method.

So, for your OP, you need two sets -

  • map<Id,Contact[]> sfdcContactsByAccountId // the keyset
  • lookerByIds // the keyset



Removes the elements in the specified set from the original set if they are present.


public Boolean removeAll(Set setOfElementsToRemove)


setOfElementsToRemove Type: Set

Return Value

Type: Boolean

This method returns true if the original set changed as a result of the call.

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.