+ Start a Discussion
kushagra gupta 23kushagra gupta 23 

Superbadge Apex Specialist

User-added image

HELPER CLASS:

public with sharing class MaintenanceRequestHelper {
public static void updateWorkOrders(List<Case> caseList) {
    List<case> newCases = new List<Case>();
    Map<String,Integer> result=getDueDate(caseList);
    for(Case c : caseList){
        if(c.status=='closed')
            if(c.type=='Repair' || c.type=='Routine Maintenance'){
                Case newCase = new Case();
                newCase.Status='New';
                newCase.Origin='web';
                newCase.Type='Routine Maintenance';
                newCase.Subject='Routine Maintenance of Vehicle';
                newCase.Vehicle__c=c.Vehicle__c;
                newCase.Equipment__c=c.Equipment__c;
                newCase.Date_Reported__c=Date.today();
                if(result.get(c.Id)!=null)
                 newCase.Date_Due__c=Date.today()+result.get(c.Id);
                else
                    newCase.Date_Due__c=Date.today();
                newCases.add(newCase);
            }
    }        
    insert newCases;   
}
//
public static  Map<String,Integer> getDueDate(List<case> CaseIDs){       
   Map<String,Integer> result = new Map<String,Integer>();
    Map<Id, case> caseKeys = new Map<Id, case> (CaseIDs);        
   List<AggregateResult> wpc=[select Maintenance_Request__r.ID cID,min(Equipment__r.Maintenance_Cycle__c)cycle
                  from Work_Part__c where  Maintenance_Request__r.ID in :caseKeys.keySet() group by             Maintenance_Request__r.ID ];
    for(AggregateResult res :wpc){
        Integer addDays=0;
        if(res.get('cycle')!=null)
            addDays+=Integer.valueOf(res.get('cycle'));
        result.put((String)res.get('cID'),addDays);
    }
        return result;
    }

 }

TRIGGER CLASS:
trigger MaintenanceRequest on Case (before update, after update) {
// ToDo: Call MaintenanceRequestHelper.updateWorkOrders
   if(Trigger.isAfter)
             MaintenanceRequestHelper.updateWorkOrders(Trigger.New);


}


TEST
@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);                   
    } 
    
}
AbhishekAbhishek (Salesforce Developers) 
Hi,

For all the Trailhead issues please report it here,

https://trailhead.salesforce.com/help?support=home#

https://trailhead.salesforce.com/help

So that our trailhead support engineers will look into it and get back to you.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Regards,
​​​​​​​Salesforce Support.