I have code to go through matching Contacts by email address, and set/synch the Email Opt Out flag on each based on it being set on one. So if Contact A, B, C have matching email, if Contact A EmailOptOut is set to TRUE, then it is also set to TRUE on Contacts B and C.

Yes, de-duplication is a goal here, hence my reasoning for this question:

When Contact A is updated, I have code to collect all matching contacts by email in a MAP. HOW then can I, when updating Contact A, set the IDs of Contact A, B, and C in a field; and then on Contact B, set the IDs of Contact A, B, and C; and the same on Contact C?

for(Contact ContactListUpdates : [SELECT ID, Email, HasOptedOutOfEmail, DoNotCall, 
                                               Email_Opt_Out_Flagged__c, Do_Not_Call_Flagged__c, DupContactIDs__c 
                                               FROM Contact where Email in :newContactMap.keySet()]) 
        contactsMap.put(ContactListUpdates.ID, ContactListUpdates);
        system.debug('Con ---' + ContactListUpdates.ID + ' -- collecting -- ' + ContactListUpdates.Email);    
        system.debug('Con --- Collection Query run  '+ contactsMap.keySet());                      

BY the end of this loop, I have a collection of all Contact records to update matched to the first record that triggered the update. The contactsMap.keySet() looks like:

10:40:54:159 USER_DEBUG [101]|DEBUG|Con --- Collection Query run  {0030R00000HFuGcQAL, 0030R00000MMZ3HQAX, 003F000002ISZKWIA5, 003F000002J5mXMIAZ}

This collection is the first ID, and the matching other 3 IDs to update. I want this exact output saved in each record's "DupContactIDs__c" field. How can I do this? Started braingstorming with the below:

Map<ID, Contact> contactsMap = new Map<ID, Contact >();
Map<ID, String> DupContactMap = new Map<ID, String>();

for (ID CID : contactsMap.keyset())
        Contact ContactDupList = contactsMap.get(CID);
        DupContactMap.put(ContactDupList.ID, contactsMap.keySet());   

You'll want to match on email, so the map would look like this:

Map<String, Set<Id>> contactsByEmail = new Map<String, Set<Id>>();

Which you'd populate in your loop:

for(Contact ContactListUpdates: ...) {
  Set<Id> relatedContacts = contactsByEmail.get(ContactListUpdates.email);
  if(relatedContacts == null) {
    contactsByEmail.put(ContactListUpdates.Email, relatedContacts = new Set<Id>());

At this point, you'd loop over the contacts again and populate the appropriate field, something like:

for(Contact record: contactsMap.values()) {
  record.DupContactIDs__c = String.join(new List<Id>(contactsByEmail.get(record.Email)),',');
  • Thanks. However for the last line (record.DupContactsIDs__c .... I get the error: Invalid initializer type Set<Id> found for List<String>: expected an Integer or a List of type String May 30 '18 at 15:22
  • @dannycheeko Yeah, I didn't specifically test this; it should have been new List<Id> instead.
    – sfdcfox
    May 30 '18 at 15:26

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.