+ Start a Discussion
Iswarya Sekar 9Iswarya Sekar 9 

Apex specialist superbadge step 4

@isTest
public class MaintenanceRequestTest {
	@isTest
    static void testMaintenanceRequest(){
        
        List<Case> caseList = new List<Case>();

        Product2 prod = new Product2();
        prod.Cost__c = 50;
        prod.Name = 'Ball Valve 10 cm';
        prod.Lifespan_Months__c = 12;
        prod.Maintenance_Cycle__c = 365;
        prod.Current_Inventory__c = 50;
        prod.Replacement_Part__c = true;
        prod.Warehouse_SKU__c = '100009';
        insert prod;
        System.assertEquals(1, [SELECT count() FROM Product2 WHERE Name = 'Ball Valve 10 cm']);
            
         for(Integer i=1;i<=200;i++) {
            Case caseNew = new Case();
            caseNew.Subject = 'Maintenance';
            caseNew.Type = 'Other';
            caseNew.Status = 'New';
            caseNew.Equipment__c = caseNew.Id;
            caseList.add(caseNew);   
        }
        
        Test.startTest();
        
        insert caseList;
        System.assertEquals(200, [SELECT count() FROM Case WHERE Type = 'Other']);
        
        for(Case a : caseList){
            a.Type = 'Repair';
            a.Status = 'Closed';
        }
		update caseList;
        System.assertEquals(200, [SELECT count() FROM Case WHERE Type = 'Repair']);
        
        Test.stopTest();
        
    }
}




public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(Map<Id, Case> applicableCases){

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

        for(Case a: applicableCases.values()){
            Case newCase = new Case();
            newCase.Vehicle__c = a.Vehicle__c;
            newCase.Equipment__c = a.Equipment__c;
            newCase.Type = 'Routine Maintenance';
            newCase.Subject = String.isBlank(a.Subject) ? 'Routine Maintenance Request' : a.Subject;
            newCase.Date_Reported__c = Date.today();
            newCase.Status = 'New';
            newCase.Product__c = a.Product__c;
            newCase.AccountId = a.AccountId;
            newCase.ContactId = a.ContactId;
            newCase.AssetId = a.AssetId;
            newCase.Origin = a.Origin;
            newCase.Reason = a.Reason;
            newCase.Date_Due__c =  (mapProduct.get(a.Id) != null) ? (Date.today().addDays(Integer.valueOf(mapProduct.get(a.Id)))) : (Date.today());
            newCases.add(newCase);
        }
        if(newCases.size() > 0){
            insert newCases;
        }
		        
        
    }        
    
}




trigger MaintenanceRequest on Case (before update, after update) {
    // call MaintenanceRequestHelper.updateWorkOrders  
    // 
    Map<Id,Case> applicableCases = new Map<Id,Case>();
    
    if(Trigger.isUpdate  && Trigger.isAfter){
        for(Case oCase: Trigger.new){
            if (oCase.IsClosed && (oCase.Type.equals('Repair') || oCase.Type.equals('Routine Maintenance'))){
                applicableCases.put(oCase.Id, oCase);
            }
        }
        if(!applicableCases.values().isEmpty()){
        	MaintenanceRequestHelper.updateWorkOrders(applicableCases);    
        }        
    } 
}

I got 100% code covergae. but i couldn't clear this challenge. the following is the error im getting when i check the challenge.
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Case: id value of incorrect type: 01t7F000003BjDmQAK: [Equipment__c].   

Can anyone please help me to fix this issue?
pconpcon
I would take a look at the exception and read it very carefully.  The whole point of the super badges is to test your knowledge and to show that you truely know the content and how to troubleshoot errors like this.
Srishti Gupta 8Srishti Gupta 8
Hi ,

For any doubt in challenge-4 of apex superspecialist, refer this URL-
https://salesforceranger.blogspot.com/2020/07/apex-specialist-challenge-4.html

I successfully completed the superspecialist badge with the help of this link.Hope it will help you too.