Apex Superbadge (part-1) – User friendly Tech help

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. Assign all profiles to the custom “HowWeRoll” page layouts for these objects. The goal is to automatically create a “Routine Maintenance” request every time a maintenance request of type “Repair” or “Routine Maintenance” is updated to “Closed”. Ensure you follow the specifications and naming conventions as outlined in the business requirements.

Solution:

  1. Rename Standard Objects:
    • Navigate to Setup > Rename Tabs and Labels > Edit (Case)
    • Rename “Case” to “Routine Maintenance” and save.
    • Repeat the above steps for “Product” and rename it to “Equipments”.
  2. Create a Routine Maintenance Request:
    • Every time a maintenance request of type “Repair” or “Routine Maintenance” is updated to “Closed”, a Routine Maintenance request should be created.
    • The skeleton for the required trigger class is already provided in the installed unmanaged package named “MaintenanceRequest”.
    • Replace the existing code with the working code base for the trigger and handler.

 


Trigger Code:

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 && p.Maintenance_Cycle__c != null){
mapProduct.put(p.Id, Integer.valueOf(p.Maintenance_Cycle__c));
}
}
for(Case a: applicableCases.values()){
Case newCase = new Case();
//... (rest of the code for creating a new case)
newCaseList.add(newCase);
}
if(newCaseList.size() > 0){
insert newCaseList;
}
}
}
Was this article helpful?
YesNo

Similar Posts