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
Thiago Barbosa 1Thiago Barbosa 1 

Error: Problem with the Trigger or test class// 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.

/* Hello, I'm with problem in the trigger or test class. 
The error this be :  System.AssertException: Assertion Failed: Expected: 402, Actual: 1204
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.

*/


trigger MaintenanceRequest on Case (before update, after update) {
    
   		List<Case> ClosedList = [SELECT Type, Status, Equipment__c, Date_Due__c, Date_Reported__c, Vehicle__c FROM Case WHERE status = 'Closed'];    
		
    	
    
    if(Trigger.isUpdate){
        for(Case c : Trigger.New){
            if(c.Type == 'Repair'  ||  c.Type =='Routine Maintenance'){
        		ClosedList.add(c);
                
                
        	}
            
    	
	    }
         
        
    }
    	MaintenanceRequestHelper.updateWorkOrders(ClosedList); 
               }
 
@isTest
public class MaintenanceRequestTest {
    
	
    @isTest static void testSetup(){
        
        
                List<Product2> equip = new List<Product2>();
        		equip.add(new Product2(Name ='Test Equipament', 
                                         Maintenance_Cycle__c = 10,
                                         Cost__c = 100,
                                         Current_Inventory__c = 10,
                                         Lifespan_Months__c = 10,
                                         Replacement_Part__c = true,
                                         Warehouse_SKU__c = 'abc'));
            
                        
                        insert equip;
        
        System.debug( 'Equip' + equip);
        
        Database.insert(equip, false);
        
        
        
    }
    
    @isTest static void testMaintenanceRequest(){
        
        
        
        MaintenanceRequestTest.testSetup();
        
        List<Case> listInsert = new List<Case>();
        
        List<Case> listUpdate = new List<Case>();
        
      	Id equipId = [SELECT Id FROM Product2 LIMIT 1].Id;
         System.debug('Product2' + equipId);
        
         Vehicle__c vhcl1 = new Vehicle__c(Name = 'VehicleTest1');
        
         Product2 prd1 = new Product2(Name = 'ProductTest1', Maintenance_Cycle__c = 2, Replacement_Part__c = true);

           for(Integer i = 0 ; i < 300; i++){
            Case caseInsert = new Case(Type='Routine Maintenance', 
                                       Status = 'New', 
                                       Origin = 'Phone',
                                       Subject = 'Routine Maintenance Request',
                                       Date_Reported__c = date.today(),
                                       Date_Due__c = date.today(),
                                       Vehicle__c = vhcl1.id,
									   Equipment__c = prd1.id);
			caseInsert.Equipment__c = equipId;            
        	
               
            listInsert.add(caseInsert);
            System.debug('List Insert' + listInsert.size());
        }
        
        
        
        Test.startTest();
        
    	Database.insert(listInsert, false);
        
        
        System.debug('Insert' + listInsert.size());
       	System.assertEquals(listInsert.size() == 300, true);
        
        
        for(Case c: listInsert){
            c.Status = 'Closed';
            listUpdate.add(c);
        }
        
        Database.update (listUpdate, false);
        
        System.assertEquals(listUpdate.size() == 300, true);
       
       
        
        User usuario = [SELECT id FROM User WHERE Id =: UserInfo.getUserId()];
              
    	System.runAs(usuario){
               
        	
             List<Case> ClosedCaseList = [SELECT Type, Status, Equipment__c, Date_Due__c, Date_Reported__c, Vehicle__c FROM Case WHERE status = 'Closed'];    
			
          	 MaintenanceRequestHelper.updateWorkOrders(ClosedCaseList);  
            
            }
        
       Test.stopTest(); 
    }
    

    
   
     	
}

 
Tiago Armando CoelhoTiago Armando Coelho
Hi Thiago, 

What is the expected behavior? 
Thiago Barbosa 1Thiago Barbosa 1
The result expected is 300, but the exercise say the error is Trigger. Could be to implement this of other form?
Thiago Barbosa 1Thiago Barbosa 1
For example, without for(Case c : Trigger.New)