My code is adding contacts in a loop. The issue I am trying to solve is that if I get more than 1 contact record with the same email address, I need to modify a custom field on the contact record instead of adding the record to the loop.

For example: This code has two contact records with the exact same details except for the contact_type field. In such a scenario, I would merge the two records and update the contact_type_field to "Customer Contact, Site Contact".

How do I approach this problem? Ideally I would like the feed itself to change so that I don't have to deal with this. At any time, I get between 10 - 15 contacts in a feed for an account. Also I need to insert all of them in one single command. I have explored the idea of upserting one contact at a time and then re-querying the database. This just leads to hitting governor limits.

List<Contact> conList = new List<Contact>();
Set<Contact> setEmails = new Set<Contact>();
Contact c = new Contact();
c.Email = 'anish.giri@mailinator.com';
c.LastName = 'Giri';
c.FirstName = 'Anish';
c.AccountId = '0011900000BjqPR';
c.Contact_Type__c = 'Customer Contact';

c = new Contact();
c.Email = 'anish.giri@mailinator.com';
c.LastName = 'Giri';
c.FirstName = 'Anish';
c.AccountId = '0011900000BjqPR';
c.Contact_Type__c = 'Site Contact';

System.Debug('Set Contents: ' + setEmails);
System.Debug('List Contents: ' + conList);
| improve this question | | | | |
  • is the code above the prod code or the testmethod code? If the latter, what does the prod code look like? – cropredy Dec 18 '15 at 22:32
  • The prod code looks different. This is a SSCCE – abhi Dec 19 '15 at 1:10

You're dealing with two problems here. Grouping by email address and then merging data between duplicates.

Using a Map<String, Contact> where the String is the contact's email address is a great way to identify duplicates as you can use the Map.containsKey(string) to figure out if the map's keyset already contains the contact's email address.

For the example below, I'm introducing another contact which won't be a duplicate to the two you've got listed.

Sample data to be scrubbed for duplicates by the code below

// List of source contacts
List<Contact> contactList = new List<Contact>();

// first contact
Contact c = new Contact();
c.Email = 'anish.giri@mailinator.com';
c.LastName = 'Giri';
c.FirstName = 'Anish';
c.AccountId = '0011900000BjqPR';
c.Contact_Type__c = 'Customer Contact';

// second contact, unrelated
Contact c1 = new Contact();
c1.Email = 'bobby.tables@example.com';
c1.LastName = 'Tables';
c1.FirstName = 'Bobby';
c1.AccountId = 'some id value'; // don't hardcode id values
c1.Contact_Type__c = 'Customer Contact';

// third contact. Will be duplicate via matching email address
Contact c2 = new Contact();
c2.Email = 'anish.giri@mailinator.com';
c2.LastName = 'Giri';
c2.FirstName = 'Anish';
c2.AccountId = '0011900000BjqPR';
c2.Contact_Type__c = 'Site Contact';

Code to find duplicates by email address

// Use a Map keyed/grouped by email address to easily find duplicates
Map<String, Contact> contactMap = new Map<String, Contact>();

// iterate the source contact list and find duplicates
for (Contact c : contactList) {

    // check for a duplicate by email address, key in the map == duplicate
    if (contactMap.containsKey(c.Email)) {

        // do not add 'c' to the Map in this case
        // this is a duplicate to a contact already found in the list
        // merge your field values as needed

        Contact originalContact = contactMap.get(c.Email);

        // use 'c' and 'originalContact'
        // choose how to merge data onto the 'originalContact' record's fields
        // originalContact.fieldName += c.fieldName; (or similar)

    } else {
        // not a duplicate, add Contact to the map under the key of its email address
        contactMap.put(c.Email, c);

// perform DML on the records that are in the map
if (contactMap.isEmpty()) {

    // get the list of Contact records from the map and insert them
    insert contactMap.values();
| improve this answer | | | | |

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.