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
Maurissa ThomasMaurissa Thomas 

Apex Specialist Superbadge help

Hello All, 

I am currently working on Apex Specialist Superbadge and Im not sure where Im going wrong. Here is the error message I am getting; 

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 5003h000002ZNFwAAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MaintenanceRequest: execution of BeforeUpdate caused by: System.NullPointerException: Argument cannot be null. Class.MaintenanceRequestHelper.updateWorkOrders: line 19, column 1 Trigger.MaintenanceRequest: line 3, column 1: []

My Apex Helper Code is as follows; 

public class MaintenanceRequestHelper {
public static void updateWorkOrders() {
        // TODO: Complete the method to update workorders
      List<Case> caseList =[
            SELECT id, Equipment__c, status, type, subject, vehicle__c, Equipment__r.Maintenance_Cycle__c
            FROM Case    
            Where Status = 'Closed' AND Type IN ('Repair','Routine Maintenance')];
   
   
    
    List<Case> newCases = new List<Case>();
          for (Case c: caseList){ 
              Case nc = new Case();
              nc.Type = 'Routine Maintenance'; 
              nc.subject = 'New Routine Maintenance';
              nc.Vehicle__c = c.Vehicle__c; 
              nc.Equipment__c = c.Equipment__c; 
              nc.Date_Reported__c = date.today(); 
              nc.Date_Due__c = date.today().addDays(Integer.valueOf(c.Equipment__r.Maintenance_Cycle__c)); 
            
              newCases.add(nc); 
          }
        
    insert newCases;
           
    }        
    
}

Please help with an explanation as I'd really like to understand what I need to do to fix this. 
Thank you all!
 
Best Answer chosen by Maurissa Thomas
TechingCrewMattTechingCrewMatt
Hello, Maurissa. I'm guessing that line 19 is:
nc.Date_Due__c = date.today().addDays(Integer.valueOf(c.Equipment__r.Maintenance_Cycle__c));
Is that corrrect? If so, it looks like the Cas does not have the Maintenance Cycle field populated. That will cause a runtime error. You can fix it by excluding Cases without the field populated as follows:
List<Case> caseList =[
            SELECT id, Equipment__c, status, type, subject, vehicle__c, Equipment__r.Maintenance_Cycle__c
            FROM Case    
            Where Status = 'Closed' AND Type IN ('Repair','Routine Maintenance') AND Equipment__r.Maintenance_Cycle__c != NULL];


 

All Answers

TechingCrewMattTechingCrewMatt
Hello, Maurissa. I'm guessing that line 19 is:
nc.Date_Due__c = date.today().addDays(Integer.valueOf(c.Equipment__r.Maintenance_Cycle__c));
Is that corrrect? If so, it looks like the Cas does not have the Maintenance Cycle field populated. That will cause a runtime error. You can fix it by excluding Cases without the field populated as follows:
List<Case> caseList =[
            SELECT id, Equipment__c, status, type, subject, vehicle__c, Equipment__r.Maintenance_Cycle__c
            FROM Case    
            Where Status = 'Closed' AND Type IN ('Repair','Routine Maintenance') AND Equipment__r.Maintenance_Cycle__c != NULL];


 
This was selected as the best answer
AbhishekAbhishek (Salesforce Developers) 
Hi,

For all the Trailhead issues please report it here,

https://trailhead.salesforce.com/en/help?support=home

So that our trailhead support engineers will look into it and get back to you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Regards,
​​​​​​​Salesforce Support.