[SalesForce] Milestones are disappearing after completion

I've setup an entitlement process with milestones (no-reoccurrence type) & entitlement, and assigned the entitlement to a case.

The process is working great, but once a milestone is complete it disappears from the Case Milestones related list.

My purpose is to collect statistical data about the milestones, where I need the ActualElapsedTime fields, which according to the answer I got here, those fields are only populated by Salesforce after completion, but my issue is that at this point the milestone disappears from the CaseMilestone object table, so I cannot collect this data.

I've tried settings the milestone type to independent, but it did not change anything. I've also tried to apply Business Hours to the entitlement, thinking it must be present for the calculation to work, but still nothing.

Is there a way to prevent this? Some configuration that I'm missing?
Unfortunately, Salesforce docs regarding the milestone functionality are not the best to say the least…

Best Answer

Scenario 1:

It might be possible that, the existing milestone is not closed (which is disappearing) before completion and one new milestone is getting created before closing the earlier milestone.

May be the milestones are getting created based on case status changes.

If you close (explicitly, through some coding or process builder) the previous milestone and then create a new milestone then all the milestones will be displayed in CaseMilestones related list.

Scenario 2:

It might be possible that, one existing milestone is running and case is getting closed without closing the milestone. In this scenario also, the milestone will be vanished from CaseMilestones related list.

Same workaround, update the milestone with completed = true and completion datetime and then close the case.

Here is the code to complete milestone:

public class MilestoneUtils 
{
    public static void completeMilestone(List<Id> caseIds, DateTime complDate) 
    {

        List<CaseMilestone> cmsToUpdate = [select Id, completionDate
                                            from CaseMilestone cm
                                            where caseId in :caseIds and IsCompleted =False
                                            and completionDate = null limit 1];
            if (cmsToUpdate.isEmpty() == false)
            {
                for (CaseMilestone cm : cmsToUpdate)
                {
                    cm.completionDate = complDate;
                }
                update cmsToUpdate;
            }
    }
}