We have intentionally created duplicate contacts in Salesforce. Importantly these records have the same email address but the rest of the fields maybe slightly unique. For the sake of argument they are duplicated as they relate to two very different business processes.

The problem is we use Email 2 Case and obviously when we duplicated our contacts the email 2 case no longer correctly relates a contact record when a match is found.

The email 2 case has multiple forwarding addresses setup and each of these relate directly to one of the contact records (with the account distinguishing the business process). This means we can match a contact record to a case received depending on the forwarding address it came from.

I hope that makes sense.

Anyway, my question is this, How can I match the correct Contact record to the case based on the "suppliedemail" of the case and another field (source)?

I have tried:

Process Buildier

I didn't get too far with this, I could define the criteria easily enough but I got stuck trying to populate the case records contactid field as I couldn't define a formula to search contact records for specific record (where suppliedEmail = contact.email && case_source__c == contact.account.name)

Workflow Rule

I gave up before starting this :(

Apex trigger

I have assumed this is the only way forward. I have tried to get started but am a little lost and would like some help. Specifically, if this is the right route (for info my logic is isolated from the trigger in it's own class, hence the following):

public static void email2caseSetContact(List<Case> newCase){
    system.debug('Executing email2caseSetContact');

    Map<Id, Contact> contactmapWin = new Map<Id, Contact>();
    Map<Id, Contact> contactmapLoss = new Map<Id, Contact>();
    List<String> emaiLListWin = new List<String>();
    List<String> emaiLListLoss = new List<String>();

    for (Case emailOnCase : newCase){
        If(emailOnCase.case_source__c== 'Win'){
        If(emailOnCase.case_source__c== 'Loss'){

    contactmapWin = new Map<Id, Contact>([SELECT id, email FROM Contact WHERE account.name = 'Winning Customers' AND email IN :emaiLListWin]);

    contactmapLoss = new Map<Id, Contact>([SELECT id, email FROM Contact WHERE account.name = 'Losing Customers' AND  email IN :emaiLListLoss]);

    for (Case record : newCase){
        if(record.ContactId == null){
            if (record.Website__c == 'Win'){
                  newCase.contact == //stuck at this point :(


Some pointers would be greatly appreciated on where to go next and also if there is a way I could avoid duplicating the code for each scenario (win vs loss).

Thanks Brian

  • N.B. Process Builder + Lightning Flow could work too as Lightning Flow can do the lookup logic and updating the ContactId on Case – cropredy Aug 2 '19 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.