function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siddhant Singh 26Siddhant Singh 26 

Challenge Not yet complete... here's what's wrong: Closing a Maintenance Request of type 'Routine Maintenance' or 'Repair' did not join the new Maintenance Request with the correct equipment.

Challenge Not yet complete... here's what's wrong:
Closing a Maintenance Request of type 'Routine Maintenance' or 'Repair' did not join the new Maintenance Request with the correct equipment. The challenge is expecting the new Maintenance Request to be joined to the same Equipment as the closed Maintenance Request.

This is my code --- 
 
public with sharing class MaintenanceRequestHelper {
    public static void updateWorkOrders(Map<String,Case> caseList) {
        System.debug('All cases>'+caseList);
        // TODO: Complete the method to update workorders
        Map<Id,Case> newCaseList = new Map<Id,Case>();
        Map<String,Date> dueDate = getDueDate(caseList);
        for (Case ca: caseList.values()) {
            Case c = new Case();
            c.Status = 'New';
            c.Origin = ca.Origin;
            //c.Equipment__c = ca.Equipment_Maintenance_Items__r.get(0).Equipment__c;
            c.Date_Reported__c = Date.today();
            c.AccountId = ca.AccountId;
            c.Reason = ca.Reason;
            c.Priority = ca.Priority;
            //System.debug('P>> '+ca.Id+'  D>> '+dueDate.get(ca.Id));
            c.Date_Due__c = dueDate.get(ca.Id);
            c.Product__c = ca.Product__c;
            System.debug('Product__c>>'+c.Product__c);
            c.Vehicle__c = ca.Vehicle__c;
            c.ProductId = ca.ProductId;
            System.debug('ProductId>>'+c.ProductId);
            c.Type = 'Routine Maintenance';
            c.AssetId = ca.AssetId;
            c.Subject = 'Scheduling Repair/Routine Maintenance';
            newCaseList.put(ca.Id, c);
        }
        if (newCaseList.size()>0)
            insert newCaseList.values();
        /*List<Equipment_Maintenance_Item__c> le = new List<Equipment_Maintenance_Item__c>();
        for (Case c: newCaseList.values()) {
            Equipment_Maintenance_Item__c e = new Equipment_Maintenance_Item__c();
            e.Maintenance_Request__c = c.Id;
            e.Equipment__c = c.Equipment__c;
            le.add(e);
        }
        insert le;*/
        System.debug('Cases = >'+newCaseList);
    }
    public static Map<String,Date> getDueDate(Map<String,Case> caseList) {
        List<AggregateResult> results = [select Maintenance_Request__c,min(Equipment__r.Maintenance_Cycle__c)cycle from Equipment_Maintenance_Item__c where Maintenance_Request__c in :caseList.keySet() group by Maintenance_Request__c];
        System.debug('>>'+results);
        Map<String, Date> dueCase = new Map<String,Date>();
        for (AggregateResult result: results) {
            if (result.get('cycle')!=null) {
                Integer m = Integer.valueOf(result.get('cycle'));
                dueCase.put(result.get('Maintenance_Request__c').toString(), Date.today().addDays(m));
            } else {
                dueCase.put(result.get('Maintenance_Request__c').toString(), Date.today());
            }
        }
        return dueCase;
    }
}
 
I've tried different methods but nothing seems to be working
VinayVinay (Salesforce Developers) 
Please note that Questions about how to pass Trailhead challenges are not on topic, because these challenges are intended to be independent demonstrations of your abilities.

Trailhead Help (https://trailhead.salesforce.com/en/help?support=home) can provide assistance for situations where Trailhead does not appear to be functioning correctly. You can reach out to them if this is the case.

Please close the thread by selected as Best Answer so that we can keep our community clean

Thanks,