You need to sign in to do that
Don't have an account?
David Cohen 15
Apex Specialist - Automate record creation challenge fail... for some reason
Hiya folks, can someone help me figure out what the heck is wrong? My code passes all my unit tests, and eveyrthing works as it should in the org...
The error:
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Update failed. First exception on row 0 with id 500360000034o9zAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of AfterUpdate caused by: System.NullPointerException: Argument cannot be null. Class.MaintenanceRequestHelper.updateWorkOrders: line 33, column 1 Trigger.MaintenanceRequest: line 12, column 1: []
The code:
Any thoughts?
Thanks very much!
The error:
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Update failed. First exception on row 0 with id 500360000034o9zAAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of AfterUpdate caused by: System.NullPointerException: Argument cannot be null. Class.MaintenanceRequestHelper.updateWorkOrders: line 33, column 1 Trigger.MaintenanceRequest: line 12, column 1: []
The code:
public class MaintenanceRequestHelper { public static void updateWorkOrders(List<Case> maintenanceRequests){ Set<Case> targetRequests = new Set<Case>(); for (Case mr : maintenanceRequests) if (('Repair' == mr.Type || 'Routine Maintenance' == mr.Type) && 'Closed' == mr.Status) targetRequests.add(mr); List<AggregateResult> results = [ SELECT Maintenance_Request__c, MIN(Equipment__r.Maintenance_Cycle__c) FROM Work_Part__c WHERE Maintenance_Request__c IN :targetRequests GROUP BY Maintenance_Request__c ]; Map<Id, Integer> requestToMinCycle = new Map<Id, Integer>(); for (AggregateResult ar : results) requestToMinCycle.put( (Id)ar.get('Maintenance_Request__c'), ((Double)ar.get('expr0')).intValue() ); List<Case> workOrders = new List<Case>(); for (Case t : targetRequests) if (('Repair' == t.Type || 'Routine Maintenance' == t.Type) && 'Closed' == t.Status) workOrders.add(new Case( Vehicle__c=t.Vehicle__c, Type='Routine Maintenance', Subject=t.Subject, Status='New', Date_Reported__c=Date.today(), Date_Due__c=Date.today().addDays(requestToMinCycle.get(t.Id)) )); if (!workOrders.isEmpty()) insert workOrders; } }The error appears to be targeting my line where assign the new Date_Due__c, implying the Case is not in my requestToMinCycle map. But I mean... everything works! And the value is populated correctly in both my unit and practical tests! So what the heck?!
Any thoughts?
Thanks very much!
David, what happens if your test data does not have a Work Part? Check it and see as the challenge check may be inserting a record without one. In addition, what happens if you have a Work Part with a valid Equipment but that Equipment record does not have a non-null Maintenance Cycle value. I hope those thoughts help.
Other food for thought: why do you need to check the type again? Do you have the Work Parts on the new Work Order? Not sure if challenges are different but verify if you should.
- I reassigned the work parts to the new case
- I checked for an empty Maintenance Cycle
- And the clinched, I added the Equipment field to my new workOrders
Thanks again