1

I have an issue where when we complete a milestone, the completion date applies to all Milestones associated with that case, rather than just the latest open Milestone. I.e. overrides all the previous milestones completion dates to system.now. I'm only just starting to learn apex and was able to locate where I think the issue is coming from, but I don't know how to fix it. This is the part of the class that seems to giving the error:

        public static void closeFirstResponseMilestone(Map<Id, Case> oldCaseMap, List<Case> caseList) {

    Set<Id> closedCasesSet = new Set<Id>();
    for (Case c : caseList) {

        if ( (c.Status != oldCaseMap.get(c.Id).Status) && (c.Status != 'Open') ) {
            closedCasesSet.add(c.Id);
        }

    }

    Map<Id, CaseMilestone> msMap = new Map<Id, CaseMilestone>();


    // May need to hard wire the FirstResponse id in here - pending feedback    
    for (CaseMilestone cm: [SELECT Id, CaseId, 
                                        CompletionDate, 
                                        MilestoneTypeId, 
                                        MilestoneType.Name 
                            FROM CaseMilestone WHERE CaseId =: closedCasesSet  ]){

        msMap.put(cm.Id, new CaseMilestone( Id = cm.Id, CompletionDate = System.now()));

    }

    if (!msMap.isEmpty()){

        update msMap.values();

    }    

}

Any help would be much appreciated.

1

You need to add isCompleted = false to find out which Milestones are open in the case and finally update the list the way you are doing.

SELECT Id, CaseId, 
    CompletionDate, 
    MilestoneTypeId, 
    MilestoneType.Name 
FROM CaseMilestone 
WHERE CaseId =: closedCasesSet
AND IsCompleted = false
| improve this answer | |
  • Hi Santanu. Thanks for the help! So that did fix the completion date issue, however it has created a new problem: when the milestone completes (with the right time and not overriding previous completed ones), it is also creating a brand new Milestone and straight away completing it as well. I.e. I'm getting a "duplicate" milestone that shouldn't exist? Does that make sense? @SantanuBoral – Priscila Aug 30 '17 at 15:46
  • This code is nothing to do this creating new milestone. You need to revisit your milestone recurrence type and logic for creating new milestones – Santanu Boral Aug 30 '17 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.