Case Milestone is created automatically if Entitlement is associated while creating the Case.
But it does not get completed until you do it manually or any automation does.
We are gonna learn in how many ways we can automate this.
- Using Flow
- Using Apex Trigger
Using Trigger
Create an Apex trigger that automatically marks milestones Completed on cases that match unique criteria. In your trigger, define which events and related case criteria must be satisfied for a milestone to be marked Completed. You can implement a similar trigger to auto-complete work order milestones.
The following triggers mark specific types of milestones Completed when the case they are on meets unique criteria. We’ve also provided a milestone utility Apex class and accompanying unit tests. Define the milestone utility class before you use any of the triggers.
- Milestone Utility Apex Class
- Apex classes reduce the size of your triggers and make it easier to reuse and maintain Apex code. To define this Apex class in your org:
- From Setup, enter Apex Classes in the Quick Find box, then click Apex Classes.
- Click New.
- Copy the class text and paste it into the text field.
- Click Save.
public class MilestoneUtils { public static void completeMilestone(List<Id> caseIds, String milestoneName, DateTime complDate) { List<CaseMilestone> cmsToUpdate = [select Id, completionDate from CaseMilestone cm where caseId in :caseIds and cm.MilestoneType.Name=:milestoneName and completionDate = null limit 1]; if (cmsToUpdate.isEmpty() == false){ for (CaseMilestone cm : cmsToUpdate){ cm.completionDate = complDate; } update cmsToUpdate; } } }
- Sample Trigger 1
- You can create a milestone named Resolution Time that requires cases to be closed within a certain length of time. It’s a great way to enforce case resolution terms in SLAs. This sample case trigger marks each Resolution Time milestone Completed when its case is closed. This way, the support agent doesn’t have to manually mark the milestone completed after closing the case.To define this trigger in your org:
- From Setup, enter Case Triggers in the Quick Find box, then click Case Triggers.
- Click New.
- Copy the trigger text and paste it into the text field.
- ClickSave.
trigger CompleteResolutionTimeMilestone on Case (after update) { if (UserInfo.getUserType() == 'Standard'){ DateTime completionDate = System.now(); List<Id> updateCases = new List<Id>(); for (Case c : Trigger.new){ if (((c.isClosed == true)||(c.Status == 'Closed'))&&((c.SlaStartDate <= completionDate)&&(c.SlaExitDate == null))) updateCases.add(c.Id); } if (updateCases.isEmpty() == false) milestoneUtils.completeMilestone(updateCases, 'Resolution Time', completionDate); } }