0

I'm writing a method that marks a case as a "Duplicate" if two or more email2case addresses are referenced in the To Address of the incoming email. However, I can't figure out the best way to test this. I know that I can just create the cases and then associate the email messages, but I would really like to verify that this will work correctly with email2case.

public static void isCaseDuplicate(List<EmailMessage> emailMessageList){
        List<EmailServicesAddress> email2CaseAddresses = [SELECT IsActive, LocalPart, EmailDomainName FROM EmailServicesAddress where Function.FunctionName='EmailToCase' and isactive = true];
        List<String> email2CaseAddressStrings = new List<String>();
        List<String> toAddressStrings = new List<String>();
        Set<String> dedupedAddress = new Set<String>();
        List<String> ccAddressStrings = new List<String>();
        List<Case> updateCases = new List<Case>();
        for(EmailServicesAddress email2Case : email2CaseAddresses){
            email2CaseAddressStrings.add(email2case.LocalPart);
        }
        system.debug('Email2Case Strings are: ' + email2CaseAddressStrings);
        integer countofE2c = 0; 
        system.debug('Initialize count should be 0: ' + countofE2c);
        for(EmailMessage emailRecord : emailMessageList){
            system.debug(emailRecord.Headers);
            if(emailRecord.ParentId != NULL && emailRecord.Incoming && string.isBlank(emailRecord.Headers)){
             system.debug('Email is Associated to Case');
                toAddressStrings = emailRecord.ToAddress.split(';');
                dedupedAddress.addAll(toAddressStrings);
                system.debug('To address size; ' + toAddressStrings.size()); 
                for(String addressString : dedupedAddress){
                    String trimmedString = addressString.subStringBefore('@');
                    system.debug('String is: ' + trimmedString.trim());
                    system.debug('Does email2case contain string? ' + email2CaseAddressStrings.contains(trimmedString.trim()));
                    if(email2CaseAddressStrings.contains(trimmedString.trim())){
                        countofE2c++;
                        system.debug('E2C Count is: ' + countofE2C);
                        
                    }
                }
                
                
            }
            if(countofE2c > 1){
                Case updateCase = new Case();
                updateCase.Id = emailRecord.ParentId; 
                updateCase.Potential_Duplicate__c = TRUE; 
                updateCases.add(updateCase);
                
            }
        }
        update updateCases;

        
        
            
    }

Saw one person recommended initializing EmailToCase the way you would an Apex Email Service but that didnt work :(

0

1 Answer 1

0

You need to simulate the way E2C works under the covers. It looks like you don't care whether the email has attachments.

  • The running user should be the user defined in Support Settings (Case Automation user)
  • Insert Case(s) using the same field values that your E2C setup has configured (Origin, Recordtype, Priority, Owner)
  • If important to you, you can coerce the assignment rules to be run when you insert the Case
  • Insert EmailMessage(s) with the fields set as you need to for testing. The EmailMessage.ParentId will reference the mocked Case(s)

A more advanced pattern would be to convert your code to use the UnitOfWork pattern and then you could dispense with actual DML entirely in your testmethod as you would only need to verify that registerDirty was called for each Case that should be updated given mocked, not DML'd EmailMessage

You must log in to answer this question.

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