+ Start a Discussion
Iswarya Sekar 9Iswarya Sekar 9 

error even after the test class covers 100% in superbadges step 4.

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: 01t7F000003BFZeQAO: [Equipment__c]
Rajesh3699Rajesh3699
Hello 

Ensure that you are passing the correct Id of case object, which starts with 500...
Hope this helps.

Thanks 
Rajesh
https://in.linkedin.com/in/rajesh-adiga-p-b3611ba7
 
Iswarya Sekar 9Iswarya Sekar 9
trigger MaintenanceRequest on Case (before update, after update) {
    // call MaintenanceRequestHelper.updateWorkOrders
    
    Map<Id,Case> applicableCases = new Map<Id,Case>();    
    if(Trigger.isUpdate){
        if(Trigger.isAfter){
            for(Case a: Trigger.new){
                if (a.IsClosed && (a.Type.equals('Repair') || a.Type.equals('Routine Maintenance'))){
                    applicableCases.put(a.Id, a);
                }
            }
        	MaintenanceRequestHelper.updateWorkOrders(applicableCases);    
        }
    } 
      
}


@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 = 345;
        prod.Current_Inventory__c = 23;
        prod.Replacement_Part__c = true;
        prod.Warehouse_SKU__c = '10000';
   //    prod.Id=caseid;
        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 = 'Maintenancerequest';
            caseNew.Type = 'Other';
            caseNew.Status = 'New';
            caseNew.Equipment__c=caseNew.Id;
            caseNew.Date_Due__c=Date.today();
            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;
        }
    }           
}
Hi Rajesh,

Plaese see the code sample. May I know where i have to change?
Rajesh3699Rajesh3699
Hello,

The error is in test class. You have to create a test record of Equipment object.
The error is in the line: caseNew.Equipment__c=caseNew.Id;
you need to provide the equipemnt record's Id not the case Id. Say for example you create a Equipment record eq, then change the line as mentioned below,

caseNew.Equipment__c=Eq.Id;


Thanks
Rajesh
https://www.linkedin.com/in/rajesh-adiga-p-b3611ba7 (https://www.linkedin.com/in/rajesh-adiga-p-b3611ba7/)
 
Iswarya Sekar 9Iswarya Sekar 9
Now also im getting error.
 
Iswarya Sekar 9Iswarya Sekar 9
Can you please give me the test class?