1

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)) {
                        continue;
                    }
                    if(con.Email.equalsIgnoreCase(lCon.Email)) {
                        //adjusts record
                        if(con.status__c == Null)
                        {
                            con.status__c = 'Billing Contact';
                            syncList.add(con);
                            //system.debug(con);

                        }
                        if(con.status__c != Null && !con.status__c.contains('Billing Contact')) {
                            con.status__c = con.status__c + ';Billing Contact';
                            syncList.add(con);
                            //system.debug(con);
                        }
                        if(con.LastName.equals('Accounts Payable') && con.status__c.contains('Billing Contact')) {
                            continue;
                        }
                           //system.debug(con);
                    }
                    else {

                    Contact bub = new Contact(LastName = 'Accounts Payable', AccountId = lCon.AccountId, status__c = 'Billing Contact', Email = lCon.Email);
                    syncList.add(bub);
                    }
6
  • you can put if check to match the Id, inside second for loop. if(con.Id != lCon.Id)
    – Sarvesh
    Commented Sep 13, 2019 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.
    Commented Sep 13, 2019 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
    Commented Sep 13, 2019 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.
    Commented Sep 13, 2019 at 18:06
  • Okay, how lookerByIds map form, is this data come from the lighting component.
    – Sarvesh
    Commented Sep 13, 2019 at 18:14

1 Answer 1

0

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

 set1.removeAll(set0);

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

    lookerByIds.keySet().removeAll(sfdcContactsByAccountId);
    

removeAll(setOfElementsToRemove)

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

Signature

public Boolean removeAll(Set setOfElementsToRemove)

Parameters

setOfElementsToRemove Type: Set

Return Value

Type: Boolean

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

You must log in to answer this question.

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