• Prem Kumar 70
  • NEWBIE
  • 30 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 4
    Replies
This is for the Apex Specialist Challenge 4. All my tests pass and code coverage is 100%.
I'm getting the error when I check the challenge.
In the debug logs they are checking the following " SOQL_EXECUTE_BEGIN [35]|Aggregations:0|SELECT COUNT() FROM Case WHERE Equipment__c = :tmpVar1" which is giving me  Assertion Failed: Expected: 402, Actual: 602. 

I've read through the log and just can't figure out what's going wrong.

Helper Code:
public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(List<case>ClosedCaseList){
        // update workorders
        
        list<case> insertCaseList = new list<case>();
        
        for(Case c : ClosedCaseList)
            
            {
                
                Case newCase = new Case();
                integer days = (integer)c.Equipment__r.Maintenance_Cycle__c;
                newCase.Type = 'Routine Maintenance';
                newCase.Status = 'New';
                newCase.Subject =  c.Subject;
                newCase.Date_Reported__c = Date.today();
                if(days != null){
                newCase.Vehicle__c = c.Vehicle__c;
                newCase.Date_Due__c = Date.today().addDays(days);
                newCase.Equipment__c = c.Equipment__c;
                }
                
                insertCaseList.add(newCase);
            }
        
        if(insertCaseList.size()>0){
        insert insertCaseList;
        }
    }
        
}

Trigger:

trigger MaintenanceRequest on Case ( after update) {
    
     List<case>ClosedCaseList = [SELECT Id, Type, Subject, Vehicle__c, Equipment__c, Equipment__r.Maintenance_Cycle__c FROM Case WHERE status = 'Closed'];
  
     List<Case> casesToUpdate = new List<case>();
    
    if(Trigger.isAfter && Trigger.isUpdate){
     for(case c : ClosedCaseList){
        
        if(c.type == 'Repair' || c.type =='Routine Maintenance'){
            
            casesToUpdate.add(c);
        }
    }
    }
    MaintenanceRequestHelper.updateWorkOrders(casesToUpdate);
}


Thanks in advance for any help.
Apex Superbadge challenge 4 error

Apex Superbadge Challenge 4 Error

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.

When I debug, I see this strange SOQL:

11:07:41.744 (4865812076)|SOQL_EXECUTE_BEGIN|[35]|Aggregations:0|SELECT COUNT() FROM Case WHERE subject LIKE '%AMC Spirit%'
11:07:41.744 (4868187164)|SOQL_EXECUTE_END|[35]|Rows:201
11:07:41.744 (4868199387)|HEAP_ALLOCATE|[35]|Bytes:4
11:07:41.744 (4868273203)|EXCEPTION_THROWN|[35]|System.AssertException: Assertion Failed: Expected: 402, Actual: 201
11:07:41.744 (4868404432)|HEAP_ALLOCATE|[35]|Bytes:48
11:07:41.744 (4868515907)|FATAL_ERROR|System.AssertException: Assertion Failed: Expected: 402, Actual: 201

Debug log

I see many people passing this challenge. Kindly help!
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,
 
I am doing the exercise portion of the Trailhead module and I copies and pasted the first Apex code into the Developer console and saved it. When I went to to the debug Execute Anonymous Window and enter the following. I got the below error message.

EmailManager em = new EmailManager();
em.sendMail('Your email address', 'Trailhead Tutorial', '123 body');

Line: 13, Column: 1
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, ID is invalid or you do not have access to the record.: [toAddresses, Your email address]

Here is the orginal code from the exercise that I copy and pasted into the Dev Console
public class EmailManager {
 
    // Public method
    public void sendMail(String address, String subject, String body) {
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {address};
        mail.setToAddresses(toAddresses);
        mail.setSubject(subject);
        mail.setPlainTextBody(body);
        // Pass this email message to the built-in sendEmail method 
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });

        // Call a helper method to inspect the returned results
        inspectResults(results);
    }
 
    // Helper method
    private static Boolean inspectResults(Messaging.SendEmailResult[] results) {
        Boolean sendResult = true;

        // sendEmail returns an array of result objects.
        // Iterate through the list to inspect results. 
        // In this class, the methods send only one email, 
        // so we should have only one result.
        for (Messaging.SendEmailResult res : results) {
            if (res.isSuccess()) {
                System.debug('Email sent successfully');
            }
            else {
                sendResult = false;
                System.debug('The following errors occurred: ' + res.getErrors());                 
            }
        }

        return sendResult;
    }
   
}