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
Sabarish Chandramouli 1Sabarish Chandramouli 1 

APEX Superbadge - Apex Specialist - Process Automation Module


Hi,

I am currently working on the "APEX Specilaist Superbadge" - first module to "Automate Mainteneace Requests" using Triggers.

This is the entity diagram given for the project:

User-added imageHere, WORK PART is the junction object that links Maintenance Request and Equipment.

This is the requirement - Need to create a case with a future date whenever an existing case status is set to "Closed".

User-added image

I have used an "Aftr Update Trigger" and this is my code - Trigger and Triggerhelper class

Trigger:

trigger MaintenanceRequest on Case (before update, after update, after insert) {
    // ToDo: Call MaintenanceRequestHelper.updateWorkOrders
    
    if(Trigger.isAfter){
        
        if(Trigger.isUpdate){
            
            MaintenanceRequestHelper mrHelper = new MaintenanceRequestHelper(Trigger.Oldmap, Trigger.newMap, Trigger.new);

            mrHelper.updateWorkOrders();           
        }

    }    
}


Trigger Helper:

public with sharing class MaintenanceRequestHelper {
    
    Map<Id, Case> oldcaseClosed = new Map<Id, Case>();
    Map<Id, Case> newCaseClosed = new Map<Id, Case>();
    List<Case> newupdatedCases = new List <Case>(); 
    List<Case> futureCases = new List<Case>();
    Set <Id> updatecaseID = new Set<Id>();
    
    
    
    //Constructor for the class
    public MaintenanceRequestHelper(Map<Id, Case> oldCases, Map<Id, Case> newCases, List<Case> triggerNew){
        
        oldcaseClosed = oldCases;
        newCaseClosed = newCases;
        newupdatedCases = triggerNew;
        
    }
    
    public void updateWorkOrders() {
        
        System.debug('Entering the build logic');
        
        for(Case csTwo :newupdatedCases){
            
            updatecaseID.add(csTwo.Id);
        }
        
        System.debug('The set size is'+updatecaseID.size());
        
        System.debug('The set elements are'+updatecaseID);
        
        //Query to pull related Maintenance cycle field from Work part using Maintenance Request ID
        
        List<AggregateResult> aggList  = [Select Maintenance_Request__c mc , MIN(Equipment__r.Maintenance_Cycle__c) emc from Work_Part__c WHERE Maintenance_Request__c IN :updatecaseID GROUP BY Maintenance_Request__c];
        
        System.debug('The aggregated results size is'+aggList.size());
        
        //List<Integer> futureDay = (List<Integer>) aggList[0].get('mc');
        
        //This Map is to get the Id and Mainteneance cycle days from SOQL and transfer it to a map
        
        Map<Id, Object> aggMap = new Map<Id, Object>();
        
        for (Integer i = 0; i< aggList.size();i++){
            for(AggregateResult agr :aggList){
                
                aggMap.put((ID)aggList[i].get('mc'),aggList[i].get('emc'));
            }
        }
        
        System.debug('The map size is'+aggMap.size());
        
        //List<Work_Part__c> workpartList = (List<Work_Part__c>)aggList;
        
        for(Case csOne :newupdatedCases){
            
            //Check only for cases of type Repair or Routine Maintenance
            
            if((csOne.Type == 'Routine Maintenance') || (csOne.Type == 'Repair')){
                
               //Run logic only if the Case status is updated from a not closed status to a closed status
               
                if((oldcaseClosed.get(csOne.Id).Status !='Closed') && (newCaseClosed.get(csOne.Id).Status =='Closed')){
                    
                    System.debug('Found the right case');
                    
                    //updatecaseID.add(csOne.Id);
                    
                    //Create Case for future date and set the fields
                    
                    Case newCase = new Case();

                    newCase.Vehicle__c = csOne.Vehicle__c;

                    newCase.Equipment__c = csOne.Equipment__c;
                    
                    newCase.Type = 'Routine Maintenance';
                    
                    newCase.Subject = 'Hey! Its time -Future maintenane';
                    
                    newCase.Date_Reported__c = System.today();
                    
                    //Use the Integer.valueof method to typecast the data that is returned as object from map
                    
                    newCase.Date_Due__c = Date.today().addDays(Integer.valueOf(aggMap.get(csOne.Id)));
                    
                    //newCase.Work_Part__c = csOne.Work_Part__c; 
                    
                    futureCases.add(newCase);
                    
                    System.debug('The items in the list is'+futureCases);
                        
               }
            }
        }
        
        if(!futureCases.isEmpty()){
            
            insert futureCases;    
        }
        
    }        
    
}

When I run it using the browser and Execute Anonymous, it is successfull and thenew case is created with a future date.

User-added image

But when I check the challenge in Trialhead, it is failing, the AggregatedResult size is 0

User-added image

User-added image

What Salesforce is doing is they are creating a new case , updating the status to "Closed" and then running it agaisnt my SOQL but my SOQL is written on the junction object - Work Part to pull the associated products used for every case and find the minimum "Maintenance Cycle Days" of all the products.

SOQL:

List<AggregateResult> aggList  = [Select Maintenance_Request__c mc , MIN(Equipment__r.Maintenance_Cycle__c) emc from Work_Part__c WHERE Maintenance_Request__c IN :updatecaseID GROUP BY Maintenance_Request__c];

Since they are inserting only a Maintenance Request and not the associated work part record, it is failing.

How do I complete this trail.

Any help would be appreciated! Thanks!