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
David Cohen 15David 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:
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!
 
Kevin CrossKevin Cross

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.

David Cohen 15David Cohen 15
Kevin, thanks for your help. I passed the challenge. What I changed:
- 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
David Cohen 15David Cohen 15
Oh, and re why I was checking the type again... I simply forgot to remove it from there after I had moved it to the top earlier. Oops.
Kevin CrossKevin Cross
Glad to hear it.  I forgot about the Equipment on the Work Order.  I found that too...the requirements of multiple Work Parts lead one to believe you do not need the singular Equipment field but seems like the test cases check for it.  In the real-world application, I would guess one also would copy the Account and Contact.  Anyway, nice job passing the challenge!
David Cohen 15David Cohen 15
Thanks. I've been progamming Apex for about two years now. And my biggest problems are with the Trailhead validations. They should probably post their test assertions in the problem so we know exactly what they're looking for. I find myself wasting too much time trying to figure out exactly what they want.
Kevin CrossKevin Cross
Same here.  I spun my wheels on this one for a while because I got this working correctly but with a Database.Batchable approach because I was reading too much into the whole bulk aspect of the requirement.  The code worked but wasn't what they were testing for, so of course it failed miserably. *laughing* Lesson learned, though.  Sometimes, it is best to keep it simple.
Amitesh PataraAmitesh Patara
Please Guide me.. I have complete Idea of this coding bt I am not getting what to do before writing this code exactly for this challenge. what objects I have to make all those things..Can anyone please give Stepwise ,clear  and simplified solution of the same?