• Rohit Shete
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I am stuck with this challenge for more than 2 days and getting this error. Please have a look at my code and let me know where i need to make a change.
Challenge Not yet complete... here's what's wrong: 
The 'MaintenanceRequest' trigger does not appear to be handling bulk operations correctly. For the positive use case of inserting and updating more than 200 records, it did not produce the expected outcome.

My helper Class : 
public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(List<Case> ClosedCaseList){
		
        List <Case> insertCaseList = new List<Case>();
        for(Case c : ClosedCaseList){
            Case newCase = new Case();
            newCase.Type = 'Routine Maintenance';
            //newCase.Subject = c.Subject;
            newCase.Status = 'New';
            newCase.Vehicle__c = c.Vehicle__c;
            newCase.Date_Reported__c = Date.today();
            newCase.Date_Due__c = date.today();
            newCase.Equipment__c = c.Equipment__c;
            insertCaseList.add(newCase);
        }
        if(insertCaseList.size()>0){
            insert insertCaseList;
        }
    }        
    
}

My Trigger : 
trigger MaintenanceRequest on Case (after update) {
    List<Case> caseList = [Select Id, Vehicle__c, Vehicle__r.Name, type, Equipment__c from Case where status = 'Closed' Limit 10];
    
    for(Case c : caseList){
        if((c.Type == 'Repair') || (c.Type == 'Routine Maintenance')){
            MaintenanceRequestHelper.updateWorkOrders(caseList);
        }    
    }
}

MaintenanceHelper 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();
    }
}

Thanks in advance. :)