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 :(