• rajesh repswal
  • NEWBIE
  • -1 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi guys, I'm almost finished with the test to get tge Apex Specialist SuperBadge, I attempt to validate the "Test automation logic" but I can't really see what is my error or why is not passing. This is the message I get:

"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."

and here is my code:

Trigger:
 
trigger MaintenanceRequest on Case (after update) {
	
	//List<Case> casesToEvaluate = new List<Case>();
	Map<Id, Case> casesToEvaluate = new Map<Id, Case>();

	if(Trigger.isAfter && Trigger.isUpdate){
		for(Case maintenance:Trigger.new){
			if((maintenance.Type.contains('Repair') || maintenance.Type.contains('Routine Maintenance')) && maintenance.Status == 'Closed'){
				casesToEvaluate.put(maintenance.Id,maintenance);
			}
		}		
	}
	MaintenanceRequestHelper.updateWorkOrders(casesToEvaluate);
}

Here's the class:
 
public class MaintenanceRequestHelper {

	public static void updateWorkOrders(Map<Id, Case>  cases){
		List<Case> maintenance_routineList = new List<Case>();

		List<Product2> listProduct = [select Id, Maintenance_Cycle__c from Product2];  
		Map<Id,decimal> mapProduct = new Map<Id, decimal>();
		
		for (Product2 p : listProduct) {
			if (p != null) {
				if(p.Maintenance_Cycle__c != null){
					mapProduct.put(p.Id, p.Maintenance_Cycle__c);
				}				
			}
		}

		System.debug('### product: '+mapProduct);

		for(Case maintenance:cases.values()){
			Case maintenanceNew = new Case();
			maintenanceNew.Subject = 'Routine Maintenance';
			System.debug('### Second: '+mapProduct.get(maintenance.Equipment__c));
			if (mapProduct.get(maintenance.Equipment__c) != null) {
				
				 maintenanceNew.Date_Due__c = Date.today().addDays(Integer.valueOf(mapProduct.get(maintenance.Equipment__c)));
				     
            }
			maintenanceNew.Vehicle__c = maintenance.Vehicle__c;
			maintenanceNew.Product__c = maintenance.Product__c;
			maintenanceNew.ContactId  = maintenance.ContactId;
			maintenanceNew.AccountId  = maintenance.AccountId;
			maintenanceNew.AssetId    = maintenance.AssetId;
			maintenanceNew.Type 	  = 'Routine Maintenance';
			maintenanceNew.Status 	  = 'New';
			maintenanceNew.Equipment__c = maintenance.Equipment__c;
			maintenanceNew.Date_Reported__c = Date.today();


			maintenance_routineList.add(maintenanceNew);
		}

		insert maintenance_routineList;
	}
}

And my testmethod:
 
@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 contact = new Contact();
		contact.FirstName = 'test';
		contact.LastName = 'last';
		contact.Email = 'test@test.com';
		contact.AccountId = acc.Id;
		insert contact;

		Vehicle__c vehicle = new Vehicle__c();
		vehicle.Name = 'car';
		insert vehicle;

		Product2 product = new Product2();
		product.Name = 'test';
		product.isActive = true;
		product.Maintenance_Cycle__c = 2;
		product.Replacement_Part__c = true;
		insert product;

		for(Integer i=1;i<=1000;i++){
			Case maintenanceNew             = new Case();
			maintenanceNew.Subject          = 'Other';
			maintenanceNew.Vehicle__c       = vehicle.Id;
			maintenanceNew.Product__c       = product.Id;
			maintenanceNew.ContactId        = contact.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);	
		}

		insert caseList;
		System.assertEquals(1000,caseList.size());

		for(Case cas:caseList){
			//update information
			cas.Type = 'Repair';
			cas.Status = 'Closed';
			secondList.add(cas);
		}

		update secondList;
		List<Case> createdCases = [Select Id from Case where Type = 'Routine Maintenance'];
		System.assertEquals(1000,createdCases.size());
	
	}	
}

Can someone help me to undestand what I'm missing?.  Thanks in advance.

Edgar,