Skip to content

Instantly share code, notes, and snippets.

@jagmohansingh
Created November 30, 2020 16:54
Show Gist options
  • Save jagmohansingh/3ca958b04912606005d66081bc6b47d2 to your computer and use it in GitHub Desktop.
Save jagmohansingh/3ca958b04912606005d66081bc6b47d2 to your computer and use it in GitHub Desktop.
Assign Entitlement Process To Incoming Cases And Mark Milestones As Completed
// case comment trigger helper class
public with sharing class CaseCommentHelper {
public static void handleFirstResponse(List<CaseComment> comments){
DateTime completionDate = System.now();
List<String> caseIds = new List<String>();
for (CaseComment cc : comments){
// Only public comments qualify
if(cc.IsPublished == true) caseIds.add(cc.ParentId);
}
if(caseIds.isEmpty() == false){
List<Case> caseList = [
select Id, ContactId, Contact.Email, OwnerId, Status,
EntitlementId, SlaStartDate, SlaExitDate
from Case
where Id IN :caseIds
];
if(caseList.isEmpty() == false){
List<Case> updateCases = new List<Case>();
for (Case caseObj : caseList) {
if (caseObj.EntitlementId != null && caseObj.SlaStartDate <= completionDate &&
caseObj.SlaStartDate != null && caseObj.SlaExitDate == null) {
updateCases.add(caseObj.Id);
}
}
if(updateCases.isEmpty() == false) {
MilestoneHelper.completeMilestone(updateCases, First Response', completionDate);
}
}
}
}
}
// case comment trigger
trigger CaseCommentTrigger on CaseComment (after insert) {
if(UserInfo.getUserType() == 'Standard'){
CaseCommentHelper.handleFirstResponse(Trigger.new);
}
}
public with sharing class CaseEntitlementHelper {
/**
* Apply entitlements from contact or account.
*/
public static void applyEntitlements(List<Case> cases){
List<Id> contactIds = new List<Id>();
List<Id> acctIds = new List<Id>();
for (Case c : cases){
if (c.EntitlementId == null && c.ContactId != null && c.AccountId != null){
contactIds.add(c.ContactId);
acctIds.add(c.AccountId);
}
}
if(!contactIds.isEmpty() || !acctIds.isEmpty()){
List<EntitlementContact> entitlementContacts = [
select e.EntitlementId, e.ContactId, e.Entitlement.AssetId from EntitlementContact e
where e.ContactId in :contactIds
and e.Entitlement.EndDate >= TODAY and e.Entitlement.StartDate <= TODAY
];
if(!entitlementContacts.isEmpty()){
for(Case c : cases){
if(c.EntitlementId == null && c.ContactId != null){
for(EntitlementContact ec : entitlementContacts){
if(ec.ContactId==c.ContactId){
c.EntitlementId = ec.EntitlementId;
if(c.AssetId==null && ec.Entitlement.AssetId!=null)
c.AssetId = ec.Entitlement.AssetId;
break;
}
}
}
}
} else {
List<Entitlement> entitlements = [
select e.StartDate, e.Id, e.EndDate, e.AccountId, e.AssetId
from Entitlement e
where e.AccountId in :acctIds and e.EndDate >= TODAY and e.StartDate <= TODAY
];
if(!entitlements.isEmpty()){
for(Case c : cases){
if(c.EntitlementId == null && c.AccountId != null){
for(Entitlement e : entitlements){
if(e.AccountId == c.AccountId){
c.EntitlementId = e.Id;
if(c.AssetId==null && e.AssetId!=null)
c.AssetId=e.AssetId;
break;
}
}
}
}
}
}
}
}
}
// trigger on Case object
trigger CaseTrigger on Case (before insert, before update) {
if(Trigger.isBefore){
if(Trigger.isInsert || Trigger.isUpdate){
CaseEntitlementHelper.applyEntitlements(Trigger.new);
}
}
}
// email message trigger helper class
public with sharing class EmailMessageHelper {
public static void handleFirstResponse(List<EmailMessage> emailMessages){
DateTime completionDate = System.now();
Map<Id, String> emIds = new Map<Id, String>();
for (EmailMessage em : emailMessages){
if(em.Incoming == false) emIds.put(em.ParentId, em.ToAddress);
}
if(emIds.isEmpty() == false){
Set<Id> emCaseIds = new Set<Id>();
emCaseIds = emIds.keySet();
List<Case> caseList = [
select Id, ContactId, Contact.Email, OwnerId,
Status, EntitlementId, SlaStartDate, SlaExitDate
from Case
where Id IN :emCaseIds
];
if(!caseList.isEmpty()){
List<Case> casesToUpdate = new List<Case>();
for (Case caseObj : caseList) {
// consider an outbound email to the contact on the case a valid first response
if (emIds.get(caseObj.Id) == caseObj.Contact.Email && caseObj.EntitlementId != null &&
caseObj.SlaStartDate <= completionDate && caseObj.SlaStartDate != null && caseObj.SlaExitDate == null) {
casesToUpdate.add(caseObj.Id);
}
}
if(!casesToUpdate.isEmpty()) {
MilestoneHelper.completeMilestone(casesToUpdate, 'First Response', completionDate);
}
}
}
}
}
// email message trigger
trigger EmailMessageTrigger on EmailMessage (before insert) {
if(Trigger.isInsert || Trigger.isAfter){
if (UserInfo.getUserType() == 'Standard'){
// complete milestone on valid first response
EmailMessageHelper.handleFirstResponse(Trigger.new);
}
}
}
// milestone helper class
public with sharing class MilestoneHelper {
public static void completeMilestone(List<Case> caseIds, String milestoneName, DateTime completionDate) {
List<CaseMilestone> toUpdate = [
select Id, CompletionDate
from CaseMilestone cm
where CaseId IN :caseIds
and cm.MilestoneType.Name = :milestoneName
and CompletionDate = null limit 1
];
if(!toUpdate.isEmpty()){
for (CaseMilestone cm : toUpdate){
cm.CompletionDate = completionDate;
}
update toUpdate;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment