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
Kellan ScheiberKellan Scheiber 

I am trying to complete the Apex Specialist SuperBadge. I am having an issue with passing the Test Class. I am using the below Class which passed but am getting an Null Pointer exception on lines 31 and 32 of my class. Any help would be appreciated.

public class MaintenanceRequestHelper {
    
    Public Static List<Case> oldCase = new List<Case>();
    Public Static List<Case> case1 = [select ID from Case Where status = 'Closed' and Type IN ('Repair', 'Routine Maintenance')];
    Public Static AggregateResult[] minCycleDay2 = [Select min(Equipment__r.Maintenance_Cycle__c) FROM Work_Part__c WHERE Equipment__r.Maintenance_Cycle__c != null
                                         AND Maintenance_Request__c IN: case1];
    
    
    public static void updateWorkOrders(List<Case> closedCase){        
        oldCase = closedCase;
   		List<Case> newCase = new List<Case>();
        
    		for (Case newCase2 : oldCase) {
            			newCase.add(new Case(type = 'Routine Maintenance',
                                status = 'New',
                                origin = 'Web',
                                Subject = null,
                                Date_Reported__c = date.today(),
                                Vehicle__c = newCase2.Vehicle__c,
                                Equipment__c = newCase2.Equipment__c,
                                Date_Due__c = cycleCalc2()));  
        }
       insert newCase;
       
       
     
      
    }
       
    private static Date cycleCalc2() {
        Integer minCycleNum = ((Decimal)minCycleDay2[0].get('expr0')).intValue();
        Date returnDate = Date.today().addDays(Integer.valueOf(minCycleNum));
        return returnDate;
        } 
        
    }
 
@isTest
private class MaintenanceRequestTest {

    @isTest static void TestWithCycleDays() {

        
             Account acct = new Account(Name= 'testAcct');
             insert acct;
             Contact cont = new Contact(LastName = 'contactTest', AccountID = acct.id, email = 'testcont@test.com');
             insert cont;
             Product2 prd1 = new Product2(Name = 'ProductTest1', Maintenance_Cycle__c = 2, Replacement_Part__c = true);
             insert prd1;
             Vehicle__c vhcl1 = new Vehicle__c(Name = 'VehicleTest1');
             insert vhcl1;
             Case MntRequest = new Case(description = 'test123',
                                Subject = 'Other',
                                type = 'Repair',
                                status = 'New',
                                origin = 'Web',
                                Date_Reported__c = date.today(),
                                Vehicle__c = vhcl1.id,
                                Equipment__c = prd1.id,
                                Date_Due__c = date.today(),
                                ContactId = cont.id,
                                AccountId = acct.id); 
        
        insert MntRequest;
        
        Work_Part__c wp1 = new Work_Part__c(Equipment__c = prd1.Id, Maintenance_Request__c = MntRequest.id);
        
        insert wp1;
        
        MntRequest.status = 'Closed';                                    
     
        update MntRequest;
        
        }
    
    @isTest static void testCaseBulkInsert(){
    List<Case> testCaseList = new List<Case>();
        For(Integer i=1 ;i<=300 ; i++) {

            Case MntRequest = new Case(
                                type = 'Routine Maintenance',
                                status = 'Closed',
                                origin = 'Web',
                                Subject = null,
                                Date_Reported__c = date.today(),
                                Vehicle__c = null,
                                Equipment__c = null,
                                Date_Due__c = date.today());
            testCaseList.add(MntRequest);
        }
            system.assertEquals(testCaseList.size(), 300);                   
    } 
    
}
 
trigger MaintenanceRequest on Case (after update, after insert) {
    
    Public List<Case> closedCase = new List<Case>();
    List<Case> caseA = [select ID, vehicle__c, equipment__c, date_due__c from Case Where Id IN :Trigger.new AND status = 'Closed' and Type IN ('Repair', 'Routine Maintenance')];
    for(Case a : caseA) {
        closedCase.add(a);
    }
           MaintenanceRequestHelper.updateWorkOrders(closedCase);  
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue and code
1) https://developer.salesforce.com/forums/?id=906F0000000kEG5IAM
2) https://developer.salesforce.com/forums/?id=906F0000000kGqUIAU
3) https://developer.salesforce.com/forums/?id=906F0000000kGPsIAM
4) https://developer.salesforce.com/forums/?id=906F0000000kG7AIAU
 
public class MaintenanceRequestHelper {
    
    Public Static List<Case> oldCase = new List<Case>();
    
    public static void updateWorkOrders(List<Case> closedCase){
        oldCase = closedCase;
   		List<Case> newCase = new List<Case>();
    		for (Case newCase2 : oldCase) {
            			newCase.add(new Case(type = 'Routine Maintenance',
                                status = 'New',
                                origin = 'Web',
                                Subject = null,
                                Date_Reported__c = date.today(),
                                Vehicle__c = newCase2.Vehicle__c,
                                Equipment__c = newCase2.Equipment__c,
                                Date_Due__c = cycleCalc2()));   
        }
        insert newCase;
    }
    
    private static Date cycleCalc2() {
        oldCase = [select ID from Case Where status = 'Closed' and Type IN ('Repair', 'Routine Maintenance')];
        AggregateResult[] minCycleDay2 = [Select min(Equipment__r.Maintenance_Cycle__c) FROM Work_Part__c WHERE Equipment__r.Maintenance_Cycle__c != null
                                         AND Maintenance_Request__c IN: oldCase];
        Integer minCycleNum = ((Decimal)minCycleDay2[0].get('expr0')).intValue();
        Date returnDate = date.Today() + minCycleNum;
        return returnDate;
               
    	}
	}

Let us know if this will help you
 
Kellan ScheiberKellan Scheiber
I ended up getting this resolved by using the below, I was originally using two SOQL queries inside my for loop has can been seen above using cycleCalc2(). When testing I was getting 100% coverage but was running into SOQL limits because of my method inside an loop. I updated and mapped out as can be seen below and passed the testing portion. I also updated my trigger to remove SOQL running inside an loop and for only after update because I was running into issues with an null pointer when including after insert. I wonder if this would lead to a validation rule that cases with an status of closed cannot be added. Going to look through above examples to see how others resolved.
trigger MaintenanceRequest on Case (after update) {
    
    Public List<Case> closedCase = new List<Case>();
    closedCase = [select ID, vehicle__c, equipment__c, date_due__c from Case Where Id IN :Trigger.new AND status = 'Closed' and Type IN ('Repair', 'Routine Maintenance')];
    MaintenanceRequestHelper.updateWorkOrders(closedCase);
}
 
public class MaintenanceRequestHelper {
        
    public static void updateWorkOrders(List<Case> closedCase){
        
        Map<Id, Date> dateToUD = getMinDate(closedCase);
   		List<Case> newCase = new List<Case>();
    		for (Case newCase2 : closedCase) {
            			newCase.add(new Case(type = 'Routine Maintenance',
                                status = 'New',
                                origin = 'Web',
                                Subject = null,
                                Date_Reported__c = date.today(),
                                Vehicle__c = newCase2.Vehicle__c,
                                Equipment__c = newCase2.Equipment__c,
                                Date_Due__c = dateToUD.get(newCase2.id)));   
        	}
        insert newCase;
    	}
    
    private static Map<ID, Date> getMinDate(List<Case> closedCase){
        closedCase = [select ID from Case Where status = 'Closed' and Type IN ('Repair', 'Routine Maintenance')];
        AggregateResult[] minCycleDay2 = [Select min(Equipment__r.Maintenance_Cycle__c) FROM Work_Part__c WHERE Equipment__r.Maintenance_Cycle__c != null
                                          AND Maintenance_Request__c IN: closedCase];
        Integer minCycleNum = ((Decimal)minCycleDay2[0].get('expr0')).intValue();
        Date datePlus = date.Today() + minCycleNum;
        Map<Id, Date> getMinDate = new Map<Id, Date>();
        for (Case a : closedCase){
            getMinDate.put(a.id, datePlus);
        }
        return getMinDate;
    }
}
 
@isTest
private class MaintenanceRequestTest {
    
    @isTest static void TestWithCycleDays() {

        
             Account acct = new Account(Name= 'testAcct');
             insert acct;
             Contact cont = new Contact(LastName = 'contactTest', AccountID = acct.id, email = 'testcont@test.com');
             insert cont;
             Product2 prd1 = new Product2(Name = 'ProductTest1', Maintenance_Cycle__c = 2, Replacement_Part__c = true);
             insert prd1;
             Vehicle__c vhcl1 = new Vehicle__c(Name = 'VehicleTest1');
             insert vhcl1;
             
        Case MntRequest = new Case(description = 'test123',
                                Subject = 'Other',
                                Type = 'Repair',
                                Status = 'Closed',
                                origin = 'Web',
                                Date_Reported__c = date.today(),
                                Date_Due__c = date.today(),
                                Vehicle__c = vhcl1.id,
                                Equipment__c = prd1.id);
            
        insert MntRequest;
        
        Work_Part__c wp1 = new Work_Part__c(Equipment__c = prd1.Id, Maintenance_Request__c = MntRequest.id);
        
        insert wp1;
        
        MntRequest.Status = 'Closed';
          
        update MntRequest; 
           
        }
    
    @isTest static void testCaseBulkInsert(){
    List<Case> testCaseList = new List<Case>();
        For(Integer i=1 ;i<=300 ; i++) {

            Case MntRequest = new Case(
                                type = 'Routine Maintenance',
                                status = 'Closed',
                                origin = 'Web',
                                Subject = null,
                                Date_Reported__c = date.today(),
                                Vehicle__c = null,
                                Equipment__c = null,
                                Date_Due__c = date.today());
            testCaseList.add(MntRequest);
        }
            system.assertEquals(testCaseList.size(), 300);                   
    } 
    
}
Kellan ScheiberKellan Scheiber
To include for the after insert I updated my trigger to the below and am at 100% coverage for testing and am no longer receiving the null pointer error. The would, as I understand, resolve issue where user would add an Case as status 'Closed' for whatever reason.
trigger MaintenanceRequest on Case (after update, after insert) {
    
    Public List<Case> closedCase = new List<Case>();
    closedCase = [select ID, vehicle__c, equipment__c, date_due__c from Case Where Id IN :Trigger.new AND status = 'Closed' and Type IN ('Repair', 'Routine Maintenance')];
    if (Trigger.isAfter && Trigger.isUpdate) {
    MaintenanceRequestHelper.updateWorkOrders(closedCase);
    }
}
Urvashi BabbarUrvashi Babbar
After being stuck on this challenge for 5 days I finally got my solution:

Please check answer at: 
https://developer.salesforce.com/forums/?id=9060G000000UZQeQAO
Suraj Tripathi 47Suraj Tripathi 47
Hi Kellan Scheiber,

//Use this code in your test Class:
@isTest
private class MaintenanceRequestHelperTest {
    
    @isTest
    static void test_Method_one(){
        
        List<case> caseList = new List<case>();
        List<case> secondList = new List<case>();
        
        Account acc = new Account();
        acc.Name = 'Test';
        insert acc;
        
        Contact con = new Contact();
        con.FirstName = 'test';
        con.LastName = 'last';
        con.AccountId = acc.Id;
        con.Email = 'test@abc.com';
        insert con;
        
        Vehicle__c vehicle = new Vehicle__c();
        vehicle.Name = 'Ford Figo';
        insert vehicle;
        
        Product2 product = new Product2();
        product.Name = 'test';
        product.Maintenance_Cycle__c = 2;
        product.IsActive = true;
        product.Replacement_Part__c = true;
        insert product;
        
        for(Integer i = 0; i<1000; i++){
            Case maintenanceNew             = new Case();
           // maintenanceNew.Subject          = 'Other';
            maintenanceNew.Vehicle__c       = vehicle.Id;
            maintenanceNew.Product__c       = product.Id;
            maintenanceNew.ContactId        = con.Id;
            maintenanceNew.AccountId        = acc.Id;
            maintenanceNew.Type             = 'Other';
            maintenanceNew.Status           = 'New';
            maintenanceNew.Equipment__c     = product.Id;
            maintenanceNew.Date_Reported__c = Date.today();
            maintenanceNew.Date_Due__c      = Date.today();

            caseList.add(maintenanceNew); 
        }
        if(caseList.size()>0){
                insert caseList;
            
        }
        
        for(case cas : caseList){
            cas.Type = 'Repair';
            cas.status = 'Closed';
            secondList.add(cas);
        }
        test.startTest();
        update secondList;
        test.stopTest();
    }
}

If you find your Solution then mark this as the best answer.  

  Thank you!

  Regards 
 Suraj Tripathi