Requirement:
Mark First Response Milestone as completed when the agent changes the status of the Case from New to Working.
Note:
IsCompleted field in CaseMilestone is not Writeable/Editable.
So, this cannot be updated.
But, to update the IsCompleted field, update the CompletionDate field. Once the CompletionDate field is updated, the IsCompleted flag will be updated.
Sample Trigger:
trigger CaseTrigger on Case ( after update ) {
Set < Id > setCaseIds = new Set < Id >();
for ( Case objCase : trigger.new ) {
if ( objCase.Status == 'Working' && trigger.oldMap.get( objCase.Id ).Status == 'New' ) {
setCaseIds.add( objCase.Id );
}
}
if ( setCaseIds.size() > 0 ) {
List < CaseMilestone > listCaseMilestones = new List < CaseMilestone >();
for ( CaseMilestone objCM : [ SELECT Id, CompletionDate FROM CaseMilestone WHERE CaseId IN: setCaseIds AND MilestoneType.Name = 'First Response' ] ) {
objCM.CompletionDate = System.now();
listCaseMilestones.add( objCM );
}
if ( listCaseMilestones.size() > 0 ) {
update listCaseMilestones;
}
}
}