I need to mark the CaseMilestone as complete and populate the completion date, when the owner of it's parent Case is changed.
For other objects I can use
List<Account> acctsToUp = new list<Account>();
for(Entitlement e:entitlements){
Account a = new Account(Id = e.AccountId);
to create a list of Accounts and then update the Account fields (I'm new to Apex and although I know the outcome of using this code, I'm not sure exactly what's happening there).
When I try to do the same in the below code, it causes an error
Field is not writeable: CaseMilestone.CaseId
and I realise that this Id can't be updated whereas the Account's can but I'm not sure what the alternative is?
When selecting the CaseMilestone(s) to update, I need to check that their MilestoneTypeId matches the Id fetched by the SOQL query.
trigger completeMilestoneOnAssignment on Case (after update) {
List<WEID__c> weids = WEID__c.getall().values();
Set<Id> validRecordTypeIds = new Set<Id>();
for (WEID__c weid: weids) {
try {
validRecordTypeIds.add(weid.WECaseId__c);
} catch (System.StringException e) {
System.debug(
System.LoggingLevel.ERROR,
'Invalid Record Type Id ' + weid.WECaseId__c
);
}
}
List<Id> caseIds = new List<Id>();
for (Case c : Trigger.new){
Case oldCase = Trigger.oldMap.get(c.Id);
if(oldCase != null && oldCase.OwnerId != c.OwnerId &&
validRecordTypeIds.contains(c.RecordTypeId))
{
caseIds.add(c.Id);
}
}
if(caseIds.isEmpty() == false){
List<MilestoneType> milestoneId = [SELECT Id
From MilestoneType
Where Name = 'Case Accepted'];
if(milestoneId.isEmpty() == false){
List<CaseMilestone> milestonesToUp = new List<CaseMilestone>();
for (Case c:caseIds){
CaseMilestone m = new CaseMilestone(CaseId = c.Id);
m.IsCompleted = TRUE;
m.CompletionDate = System.now();
}
update milestonesToUp;
}
}
}