+ Start a Discussion
Rameshwar gaurRameshwar gaur 

apex specialist badge

i get that error on automated record creation

Challenge Not yet complete... here's what's wrong: 
Inserting a new Maintenance Request of type 'Routine Maintenance' and then closing it did not create of a new Maintenance Request based upon the original record correctly. The challenge is expecting to find the closed Maintenance Request plus an 'New' Maintenance Request of type 'Routine Maintenance' with the same Vehicle as the closed one.


This is trigger code
trigger MaintenanceRequest on Case (before update, after update) {
    // call MaintenanceRequestHelper.updateWorkOrders  
    if(Trigger.isUpdate)
    {
       MaintenanceRequestHelper.updateWorkOrders(trigger.new , trigger.oldmap); 
    }
    
}

And trigger helper class
 
public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(list<case> triggerlist,map<id,case> triggermap){
        // update workorders
        list<case> FutureMaintenanceList = new list<case>();
        list<product2> prdlst = new list<product2>();
         try
         {
            prdlst =new list<product2>([SELECT Cost__c,Id,IsActive,Lifespan_Months__c,Maintenance_Cycle__c,Name,ProductCode,Replacement_Part__c FROM Product2]);
        	}
        catch(exception e)
        {
            System.debug('Error Caught !!! '+e.getMessage());
        }
        map<id,product2>  equipment = new map<id,product2>(prdlst);
        
        for(case cs:triggerlist)
        {
            if(triggermap.get(cs.id).status != 'closed' && cs.Status == 'closed')
            {
                    case NewMaintenance = new case();
                    date Todaydate = date.today();
             	    NewMaintenance.AccountId = cs.AccountId;
              	    NewMaintenance.ContactId = cs.ContactId;
              	    NewMaintenance.AssetId = cs.AssetId;
                    NewMaintenance.Subject ='Vehicle '+cs.Vehicle__c+' future Maintenance ';
                    NewMaintenance.Vehicle__c=cs.Vehicle__c;
                 	NewMaintenance.Date_Reported__c= Todaydate ;
                 	NewMaintenance.Equipment__c = cs.Equipment__c;
                    NewMaintenance.Type = 'Routine Maintenance';
                    NewMaintenance.Status = 'New';
                    NewMaintenance.Origin =cs.Origin;
                    NewMaintenance.Date_Due__c=Todaydate.adddays(integer.valueOf(math.round(equipment.get(cs.Equipment__c).Maintenance_Cycle__c)));
                    FutureMaintenanceList.add(NewMaintenance);
                integer x =integer.valueOf(math.round(equipment.get(cs.Equipment__c).Maintenance_Cycle__c));
                system.debug('value--'+x);
            }
        }
        system.debug('FutureMaintenanceList--'+FutureMaintenanceList);
        if(FutureMaintenanceList.size() >0)
        {
          insert FutureMaintenanceList;  
        }
       
    }        
    
}
Can any one tell me what i doing wrong.
Thank You

​​​​​​​
Raj VakatiRaj Vakati
Use this code
 
trigger MaintenanceRequest on Case (before update, after update) {
    if (Trigger.isAfter) {
        MaintenanceRequestHelper.updateWorkOrders(Trigger.oldMap, Trigger.newMap);
    }
}
 
public class MaintenanceRequestHelper {
    
    private static final String REQUEST_TYPE = 'Routine Maintenance';
    private static final String NEW_STATUS = 'New';
    private static final String REQUEST_SUBJECT = 'routine checkup';
    private static final String REQUEST_ORIGIN = 'Web';
    private static final String CLOSED = 'Closed';
    private static final String REPAIR = 'Repair';
    private static final String ROUTINE_MAINTENANCE = 'Routine Maintenance';
    private static List<Work_Part__c> requestWorkParts;
    
    public static void updateWorkOrders(Map<Id, Case> oldMaintenanceRequestMap, Map<Id, Case> newMaintenanceRequestMap) {
        requestWorkParts = new List<Work_Part__c>();
        Map<Id, Case> oldIdNewRequest = new Map<Id, Case>();
        List<Case> insertCaseList  = new List<Case>();
        List<Work_Part__c> allWorkParts = [SELECT Maintenance_Request__c, Equipment__r.Maintenance_Cycle__c 
                                           FROM Work_Part__c 
                                           WHERE Maintenance_Request__c =: oldMaintenanceRequestMap.keySet()];
        for (Id oldRequestId : oldMaintenanceRequestMap.keySet()) {
            Case oldRequest = oldMaintenanceRequestMap.get(oldRequestId);
            Case newRequest = newMaintenanceRequestMap.get(oldRequestId);
            if ( (oldRequest.Status != CLOSED) && 
                 (newRequest.Status == CLOSED) &&
                 ( (oldRequest.Type == REPAIR) || (oldRequest.Type == ROUTINE_MAINTENANCE) ) ) {
                
                Case newMaintenanceRequest = createNewRequest(oldRequest, calculateMaintenanceCycle(oldRequestId, allWorkParts));
                oldIdNewRequest.put(oldRequestId, newMaintenanceRequest);
                insertCaseList.add(newMaintenanceRequest);
            }
        }

        insert insertCaseList; 

        for (Work_Part__c workPart : requestWorkParts) {
            workPart.Maintenance_Request__c = oldIdNewRequest.get(workPart.Maintenance_Request__c).Id;
        } 
        update allWorkParts;  
    }

    private static Date calculateMaintenanceCycle(Id oldRequestId, List<Work_Part__c> allWorkParts) {
        for(Work_Part__c wp : allWorkParts) {
            if (wp.Maintenance_Request__c == oldRequestId) {
                requestWorkParts.add(wp);
            }
        }
        List<Decimal> maintenanceCycleDays = new List<Decimal>();
        Integer shortestMaintenanceCycleDay = 0;
        if (requestWorkParts.size() > 0) {
            for (Work_Part__c wp : allWorkParts) {
                maintenanceCycleDays.add(wp.Equipment__r.Maintenance_Cycle__c);
            }
            maintenanceCycleDays.sort();
            shortestMaintenanceCycleDay = maintenanceCycleDays[0].intValue();
        }
        Date maintenanceCycleDate = Date.today().addDays(shortestMaintenanceCycleDay);
        return maintenanceCycleDate;
    } 

    private static Case createNewRequest(Case oldRequest, Date maintenanceCycleDate) {
        Id vehicle = oldRequest.Vehicle__c; 
        Id equipment = oldRequest.Equipment__c;
        String requestOrigin = oldRequest.Origin;
        String requestSubject = String.isBlank(oldRequest.Subject) ? REQUEST_SUBJECT : oldRequest.Subject;
        Date reportDate = Date.today();
        Case newRequest = new Case(Type = REQUEST_TYPE, 
                                   Subject = requestSubject,  
                                   Origin = requestOrigin,
                                   Status = NEW_STATUS,
                                   Equipment__c = equipment,
                                   Date_Reported__c = reportDate,  
                                   Vehicle__c = vehicle,
                                   Date_Due__c = maintenanceCycleDate);
        return newRequest;
    }
}

 
Rameshwar gaurRameshwar gaur
Still got the same error.