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
Apurv SaxenaApurv Saxena 

Apex Specialist Challange 4:

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. :)
SandhyaSandhya (Salesforce Developers) 
Hi,

Please refer below links for similar discussion.

https://developer.salesforce.com/forums/?id=906F0000000kFjcIAE
 
http://salesforce.stackexchange.com/questions/130242/superbadges-apex-specialist-the-maintenancerequest-trigger-does-not-appear
 
Hope this helps you!

If this helps you mark it as solved.

Thanks and Regards
Sandhya
Abhishek Kumar 793Abhishek Kumar 793
Hi ,

Please check below Github url, Follow Challenge 1 and 3, you will pass the challenge by the help of below code

https://github.com/abhishekcse122/Apex-Specialist-Superbadge

Mark this as best result if this helps you :)

Thanks
Rohit SheteRohit Shete
I am stuck with this challenge for more than 4 days and getting this error. Please have a look at my code and let me know where i need to make a change.


BELOW IS MY CODE



public class MaintenanceRequestHelper {

public static void updateWorkOrders(){
// update workorders
//bulk determine
List<case> maintenanceRequestList=[select id,case.Vehicle__c,Equipment__c,Equipment__c.Maintenance_Cycle__c,Type,Status from case where id in :Trigger.New limit 200];
if(maintenanceRequestList !=null && maintenanceRequestList.size()>0 ){
List<Case> insertMaintenanceRequest=getCaseList(maintenanceRequestList);
insert insertMaintenanceRequest;
}
}

public static List<Case> getCaseList(List<Case> maintenanceRequestList){
List<Case> newMaintenanceRequestList= new List<Case>();
for(Case cas:maintenanceRequestList){
if(cas.Type=='Routine Maintenance' && cas.Status=='Closed'){
case newMaintenanceRequest=new Case();
newMaintenanceRequest.Subject='test';
newMaintenanceRequest.Type='Routine Maintenance';
newMaintenanceRequest.Vehicle__c=cas.Vehicle__c;
newMaintenanceRequest.Equipment__c=cas.Equipment__c;
newMaintenanceRequest.Date_Reported__c=date.Today();
newMaintenanceRequest.Date_Due__c=Date.today().addDays(Integer.valueOf(cas.Equipment__c.Maintenance_Cycle__c));
newMaintenanceRequest.Status='New';
newMaintenanceRequest.Origin='Phone';
newMaintenanceRequestList.add(newMaintenanceRequest);
}
}
return newMaintenanceRequestList;
}
}



-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


@isTest
public class MaintenanceRequestTest {

@isTest static void testMaintenanceRequest(){

List<Case> maintenanceList=new List<Case>();
List<Case> maintenanceListAfterClosed=new List<Case>();
Vehicle__c vehicle=new Vehicle__c(Name='tata sumo',Air_Conditioner__c=true,Model__c='23Test');
insert vehicle;
Product2 equipment=new Product2(Name='tire',Cost__c=100,Current_Inventory__c =10,Replacement_Part__c=true,
Warehouse_SKU__c ='test',Lifespan_Months__c =10,Maintenance_Cycle__c=10);
insert equipment;
for(Integer i=1;i<=300;i++){
Case maintenance=new Case(Subject='Test subject'+i,Type='Routine Maintenance'+i,Status='New'+i,
Origin='Phone'+i,Equipment__c=equipment.Id,Vehicle__c=vehicle.Id);
maintenanceList.add(maintenance);
}
insert maintenanceList;
// system.assertEquals(300, maintenanceList.size());
for(Case caseupdate:maintenanceList){
caseupdate.Status='Closed';
caseupdate.Type='Routine Maintenance';
caseupdate.Date_Due__c=date.Today().addDays(Integer.valueOf(equipment.Maintenance_Cycle__c));
maintenanceListAfterClosed.add(caseupdate);
}
Test.startTest();
//UPDATE maintenanceListAfterClosed;
//Bulk insert update
Database.SaveResult[] updatequipment = Database.update(maintenanceListAfterClosed);
Test.stopTest();
for(Database.SaveResult sa:updatequipment){
System.assert(sa.isSuccess());}
}
}





------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

trigger MaintenanceRequest on Case (before update, after update) {

// call MaintenanceRequestHelper.updateWorkOrders

MaintenanceRequestHelper.updateWorkOrders();

}
Suraj Tripathi 47Suraj Tripathi 47
Hi Apurv,

You don't need to query in this trigger, you can use your condition in this trigger
You can use this code:
trigger MaintenanceRequest on Case (after update) { 
            MaintenanceRequestHelper.updateWorkOrders(trigger.new);
}

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

  Thank you!
  Regards 
 Suraj Tripathi