Created
July 20, 2017 16:19
-
-
Save tontonleric/4ed8d10d6610dfe4bbde27980627827f to your computer and use it in GitHub Desktop.
Email to case management class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* <b>Classe AP13_EmailToCaseManagement :</b> email to case management. | |
* @UseCase : When a customer send an email to multiple recipient, that email will generate multiple claim. | |
* @BusinessNeed : If multiple claim are created from one email to case, then the first case become the parent case and the other claim are cancelled | |
* @author Eric Wartelle | |
* @date 05/07/2017 | |
*/ | |
public with sharing class AP13_EmailToCaseManagement { | |
//Methods of the class AP13_EmailToCaseManagement | |
/** | |
* <b>Method getIdKey </b>: return an IdKey that will help us determine case coming from the same email | |
* @param newCase Case : a newly created case. | |
* @return String : the key will have the following form SuppliedEmail|Subject|CreatedDate; | |
*/ | |
private static String getIdKey(Case newCase){ | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## getIdKey Start'); | |
String result; | |
if(newCase.SuppliedEmail != null || newCase.SuppliedEmail != ''){ | |
//Variables declaration | |
result = newCase.SuppliedEmail + '_' + newCase.Subject; | |
} | |
else{ | |
result = 'unrecordedEmail_' + newCase.Subject + '_' + | |
Date.today().year() + '_' + Date.today().month() + '_' + Date.today().day(); | |
} | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## Generate ID Key : ' + result); | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## getIdKey End'); | |
//Return the result | |
return result; | |
} | |
/** | |
* <b>Method getiDKeyToListCaseMapping</b>: return a mapping between case and the common IDKey | |
* @param caseMap Map<Id, Case> : cases sent by the trigger. | |
* @return Map<String, Set<Case>> : the corresponding Mapping | |
*/ | |
public static Map<String, Set<Case>> getiDKeyToListCaseMapping(Map<Id, Case> casesMap){ | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## getiDKeyToListCaseMapping start'); | |
//Declaration de variables | |
Map<String, Set<Case>> result = new Map<String, Set<Case>>(); | |
//For each case in the map, we're going to generate an IDKey. | |
//If there is a list of case for these IDKey, we're putting the case in it. | |
//If there is no list, we're putting the case in it | |
Set<Id> casesMapKeySet = casesMap.keySet(); | |
for(ID casesMapKey : casesMapKeySet){ | |
Case currentCase = casesMap.get(casesMapKey); | |
String currentCaseIDKey = AP13_EmailToCaseManagement.getIdKey(currentCase); | |
if(! result.containsKey(currentCaseIDKey)){ | |
result.put(currentCaseIDKey, new Set<Case>()); | |
} | |
result.get(currentCaseIDKey).add(currentCase); | |
} | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## result.size() : ' + result.size()); | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## getiDKeyToListCaseMapping start'); | |
//Returning the result | |
return result; | |
} | |
/** | |
* <b>Method getCaseToUpdate </b>: return the case to update. | |
* @param validateCase Set<Case> : the case we need to update | |
* @return List<Case> : the updated case. | |
*/ | |
private static List<Case> getCaseToUpdate(Set<Case> validateCase){ | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.DEBUG, '## AP13_EmailToCaseManagement ## getCaseToUpdate Start'); | |
//Variable Declaration | |
List<Case> validateCaseList = new List<Case>(validateCase); | |
List<Case> result = new List<Case>(); | |
Id parentCaseId; | |
Boolean parentCaseDetected = false; | |
Integer parentCaseIndex = 0; | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.DEBUG, '## AP13_EmailToCaseManagement ## validateCaseList.size() : ' + validateCaseList.size()); | |
//Finding the parent case | |
parentCaseId = validateCaseList.get(0).Id; | |
for(integer index = 1; index < validateCaseList.size(); index++){ | |
Case tempCase = validateCaseList.get(index); | |
//Until we found a parent case | |
if(! parentCaseDetected){ | |
if(tempCase.ParentId != null){ | |
parentCaseId = tempCase.ParentId; | |
parentCaseDetected = true; | |
parentCaseIndex = index; | |
} | |
} | |
} | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## parentCaseIndex : ' + parentCaseIndex); | |
//Updating each case to Cancelled if necessary and pointing them to the selected parent case | |
//parentCaseId = validateCaseList.get(parentCaseIndex).Id; | |
for(integer index = 0; index < validateCaseList.size(); index++){ | |
Case tempCase = validateCaseList.get(index); | |
//Until we found a parent case | |
if(tempCase.Id != parentCaseId){ | |
tempCase.Status = EM001_Case.cancelledStatus; | |
tempCase.ParentId = parentCaseId; | |
tempCase.TECH_SharedEmail__c = true; | |
tempCase.IsStopped = true; | |
tempCase.Entitlement = null; | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## tempCase.ParentId ' + tempCase.ParentId); | |
result.add(tempCase); | |
} | |
} | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.DEBUG, '## AP13_EmailToCaseManagement ## getCaseToUpdate Stop'); | |
//Return result | |
return result; | |
} | |
/** | |
* <b>Method emailToCaseManagement </b>: will apply the business need. Will be called by the trigger. | |
* @param newCases Map<Id, Case>: the newly created case. | |
*/ | |
public static void emailToCaseManagement(Map<Id, Case> newCases) { | |
//System.Debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## emailToCaseManagement Start'); | |
//Sephamore to lock the execution | |
Lock__c lock = [SELECT ID FROM Lock__c FOR UPDATE]; | |
update(lock); | |
//Starting by getting all the case created today | |
Map<Id, Case> casesCreatedToday = new Map<Id, Case>([SELECT Id, SuppliedEmail, Subject, CreatedDate, ParentId, CaseNumber FROM Case WHERE CreatedDate = TODAY ORDER BY CaseNumber]); | |
//System.debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## casesCreatedToday.size() : ' + casesCreatedToday.size()); | |
//Mapping between a list of case and the corresponding list of case. | |
Map<String, Set<Case>> iDKeyToListOldCaseMapping = AP13_EmailToCaseManagement.getiDKeyToListCaseMapping(casesCreatedToday); | |
Map<String, Set<Case>> iDKeyToListNewCaseMapping = AP13_EmailToCaseManagement.getiDKeyToListCaseMapping(newCases); | |
//Merging of the two | |
iDKeyToListNewCaseMapping.putAll(iDKeyToListOldCaseMapping); | |
//For each list of the map, we're going to check the case and update them if necessary | |
Set<String> keySet = iDKeyToListNewCaseMapping.keySet(); | |
System.debug(LoggingLevel.DEBUG, '## AP13_EmailToCaseManagement ## keySet.size() : ' + keySet.size()); | |
//System.debug(LoggingLevel.DEBUG, '## AP13_EmailToCaseManagement ## iDKeyToListNewCaseMapping.' + iDKeyToListNewCaseMapping.); | |
List<Case> caseToUpdate = new List<Case>(); | |
for(String key : keySet){ | |
System.Debug(LoggingLevel.DEBUG, '## AP13_EmailToCaseManagement ## key : ' + key); | |
Set<Case> casesSet = iDKeyToListNewCaseMapping.get(key); | |
caseToUpdate.addAll(AP13_EmailToCaseManagement.getCaseToUpdate(casesSet)); | |
} | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## caseToUpdate.size() ' + caseToUpdate.size()); | |
update(caseToUpdate); | |
//System.Debug call for debug log purpose | |
System.debug(LoggingLevel.INFO, '## AP13_EmailToCaseManagement ## emailToCaseManagement End'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment