[SalesForce] How to stop recurrence milestone in apex

Hi I am using sequential milestone in our project.

We also have initial response and resolution time milestones working on case.
Sequential milestone we used to send an update to the customer between specified time. so when we send first update it gets stopped and reschedule again. It works perfectly.

The problem is when case is closed at that time resolution time milestone is get stopped but update(sequential) milestone still running. It doesn't make any sense to send the update when case is closed

Could anyone provide me the solution how to stop this sequential milestone completely after closing the case??

Best Answer

I faced this issue earlier. Generally, you can close the Milestones by a trigger that automatically marks milestones Completed on cases that match unique criteria. But in case of Sequential Milestone, it will create another Milestone as soon as you close one. To resolve this, I removed the Entitlement from the Case as soon as it closes. It worked for me.

Examples as mentioned in the Salesforce Help Article. The change I made is c.EntitlementId = null;

Trigger

trigger CompleteResolutionTimeMilestone on Case (before 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);
                c.EntitlementId = null;
            }
        }
        if (updateCases.isEmpty() == false){
            milestoneUtils.completeMilestone(updateCases, 'Every 10 Minute Response', completionDate);
        }
    }
}

Util Class

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;
        }
    }
}
Related Topic