Skip to main content The new Salesforce certifications experience is live! Visit Trailhead Academy to explore your new certifications homepage.
I have the need to complete a milestone once the sales rep sends the first email (not the auto-reply when the case is created).

 

I tried to implement the trigger suggested in the entitlement management guide but it doesn't work, so is there something to change there or would be possible to complete it automatically via Process Builder?

 

Here the code used:

 

 

trigger CompleteFirstResponseEmail on EmailMessage (after insert) {

if (UserInfo.getUserType() == 'Standard'){

DateTime completionDate = System.now();

Map<Id, String> emIds = new Map<Id, String>();

for (EmailMessage em : Trigger.new){

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 c.Id, c.ContactId, c.Contact.Email,

c.OwnerId, c.Status,

c.EntitlementId,

c.SlaStartDate, c.SlaExitDate

From Case c where c.Id IN :emCaseIds];

if (caseList.isEmpty()==false){

List<Id> updateCases = new List<Id>();

for (Case caseObj:caseList) {

if ((emIds.get(caseObj.Id)==caseObj.Contact.Email)&&

(caseObj.Status == 'In Progress')&&

(caseObj.EntitlementId != null)&&

(caseObj.SlaStartDate <= completionDate)&&

(caseObj.SlaStartDate != null)&&

(caseObj.SlaExitDate == null))

updateCases.add(caseObj.Id);

}

if(updateCases.isEmpty() == false)

milestoneUtils.completeMilestone(updateCases,

'First Response', completionDate);

}

}

}

}

 

 
1 answer
0/9000