Apex Superbadge (part-1)

Scenario:- Automate record creation

Install the unmanaged package for the schema and stubs for Apex classes and triggers. Rename cases and products to match the HowWeRoll schema, and assign all profiles to the custom HowWeRoll page layouts for those objects. Use the included package content to automatically create a Routine Maintenance request every time a maintenance request of type Repair or Routine Maintenance is updated to Closed. Follow the specifications and naming conventions outlined in the business requirements.

Solution:-

  1. Rename standard objects > Case, Product

Setup > Rename Tabs and Labels > Edit (Case) > Rename to “Routine Maintenance”  > Save

Repeat above for Product and rename to Equiuements

2. Create a Routine Maintenance request every time a maintenance request of type Repair or Routine Maintenance is updated to Closed.

Skelton for the required trigger class is already coming from installed unmanaged package > MaintenanceRequest

trigger MaintenanceRequest on Case (before update, after update) {
    // call MaintenanceRequestHelper.updateWorkOrders  
}

Replace this with working code base Trigger:-

trigger MaintenanceRequest on Case (before update, after update) {
    // call MaintenanceRequestHelper.updateWorkOrders  
    Map<Id,Case> caseLst = 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'))){
                caseLst.put(oCase.Id, oCase);
            }
        }
        if(caseLst.size() > 0){
            System.debug('*******Calling updateWorkOrders from MaintenanceRequestHelper Class*******');
        	MaintenanceRequestHelper.updateWorkOrders(caseLst);    
        }        
    } 
}

Handler code “MaintenanceRequestHelper” :-

public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(Map<Id, Case> applicableCases){
		System.debug('*******Inside MaintenanceRequestHelper Class*******');
	    Map<Id, Integer> mapProduct = new Map<Id, Integer>(); 
   		List<Case> newCaseList = 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());
            newCaseList.add(newCase);
        }
        if(newCaseList.size() > 0){
            insert newCaseList;
        }
		        
        
    }        
    
}

Leave a Reply

Your email address will not be published. Required fields are marked *