I cannot resolve this error.
Closing a Maintenance Request of type 'Routine Maintenance' or 'Repair' did not create of a new Maintenance Request with the correct due date. The challenge is expecting the due date to be calculated using the maintenance cycle defined on the related equipment records. If multiple equipments are used in the maintenance request, choose the shortest maintenance cycle to define the service date.
I'm having a really hard time finishing this challenge. I want to know that which part of my code is wrong, not just answer.
// TriggerHandler
public with sharing class MaintenanceRequestHelper {
public static void updateWorkOrders(Map<Id, Case> oldCases) {
// TODO: Complete the method to update workorders
Map<Id, Integer> toGetDueDateMap = new Map<Id, Integer>();
AggregateResult[] results = [SELECT Id, MIN(Maintenance_Cycle__c) minMC FROM Product2 GROUP BY Id];
for (AggregateResult ar : results) {
if (ar != null) {
toGetDueDateMap.put(ar.Id, Integer.valueOf(ar.get('minMC')));
}
}
List<Case> newCases = new List<Case>();
for (Case c : oldCases.values()) {
Case newCase = new Case();
newCase.Status = 'New';
newCase.Origin = 'web';
newCase.Vehicle__c = c.Vehicle__c;
newCase.ProductId = c.ProductId;
newCase.Type = 'Routine Maintenance';
newCase.Subject = 'Routine Maintenance';
newCase.Date_Reported__c = Date.today();
newCase.Date_Due__c = (toGetDueDateMap.get(c.Id) != null) ? Date.today().addDays(toGetDueDateMap.get(c.Id)) : Date.today();
newCases.add(newCase);
}
insert newCases;
}
}
// Trigger
trigger MaintenanceRequest on Case (after update) {
// ToDo: Call MaintenanceRequestHelper.updateWorkOrders
Map<Id, Case> caseToEvaluate = new Map<Id, Case>();
if(Trigger.isAfter && Trigger.isUpdate) {
for(Case c : Trigger.New) {
if(c.Status == 'Closed' && (c.Type == 'Repair' || c.Type == 'Routine Maintenance')) {
caseToEvaluate.put(c.Id, c);
}
}
}
MaintenanceRequestHelper.updateWorkOrders(caseToEvaluate);
}
I create correct algoritm to find less maintenance cycle days. Try it.
newCase.Date_Due__c = Date.today() + suchEarlyDay(newMaintenanceRequest).get(cases.Id);
newCases.add(newCase);
}
}
if (newCases.size() > 0) {
insert newCases;
}
}
private static Map<Id, Integer> suchEarlyDay(List<Case> cases) {
Map<Id, Case> caseMap = new Map<Id, Case>(cases);
Map<Id, Integer> lessDate = new Map<Id, Integer>();
List<Equipment_Maintenance_Item__c> relatedList = [
SELECT Maintenance_Request__c,
Equipment__r.Maintenance_Cycle__c
FROM Equipment_Maintenance_Item__c
WHERE Maintenance_Request__c = :caseMap.keySet()
];
for (Id caseId : caseMap.keySet()) {
for (Equipment_Maintenance_Item__c equip : relatedList) {
if (!lessDate.containsKey(caseId) && caseId == equip.Maintenance_Request__c) {
lessDate.put(caseId, (Integer) equip.Equipment__r.Maintenance_Cycle__c);
} else if (lessDate.containsKey(caseId) && lessDate.get(caseId) > (Integer) equip.Equipment__r.Maintenance_Cycle__c) {
lessDate.put(caseId, (Integer) equip.Equipment__r.Maintenance_Cycle__c);
}
}
}
return lessDate;
}