Apex Superbadge (part-1) – User friendly Tech help
n
Scenario:- Automate record creation
n
n
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.
n
Solution:-
n
n
n
- Rename standard objects > Case, Product
n
n
n
Setup > Rename Tabs and Labels > Edit (Case) > Rename to “Routine Maintenance” > Save
n
n
Repeat above for Product and rename to Equiuements
n
n
2. Create a Routine Maintenance request every time a maintenance request of type Repair or Routine Maintenance is updated to Closed.
n
n
Skelton for the required trigger class is already coming from installed unmanaged package > MaintenanceRequest
n
trigger MaintenanceRequest on Case (before update, after update) {n // call MaintenanceRequestHelper.updateWorkOrders n}
n
Replace this with working code base Trigger:-
n
trigger MaintenanceRequest on Case (before update, after update) {n // call MaintenanceRequestHelper.updateWorkOrders n Map caseLst = new Map();n n if(Trigger.isUpdate && Trigger.isAfter){n for(Case oCase: Trigger.new){n if (oCase.IsClosed && (oCase.Type.equals('Repair') || oCase.Type.equals('Routine Maintenance'))){n caseLst.put(oCase.Id, oCase);n }n }n if(caseLst.size() > 0){n System.debug('*******Calling updateWorkOrders from MaintenanceRequestHelper Class*******');n MaintenanceRequestHelper.updateWorkOrders(caseLst); n } n } n}n
n
Handler code “MaintenanceRequestHelper” :-
n
public class MaintenanceRequestHelper {n n public static void updateWorkOrders(Map applicableCases){n System.debug('*******Inside MaintenanceRequestHelper Class*******');n Map mapProduct = new Map(); n List newCaseList = new List();n n List listProduct = [select Id, Maintenance_Cycle__c from Product2]; n for (Product2 p : listProduct) {n if (p != null) {n if(p.Maintenance_Cycle__c != null){n mapProduct.put(p.Id, Integer.valueOf(p.Maintenance_Cycle__c));n } n }n }nn for(Case a: applicableCases.values()){n Case newCase = new Case();n newCase.Vehicle__c = a.Vehicle__c;n newCase.Equipment__c = a.Equipment__c;n newCase.Type = 'Routine Maintenance';n newCase.Subject = String.isBlank(a.Subject) ? 'Routine Maintenance Request' : a.Subject;n newCase.Date_Reported__c = Date.today();n newCase.Status = 'New';n newCase.Product__c = a.Product__c;n newCase.AccountId = a.AccountId;n newCase.ContactId = a.ContactId;n newCase.AssetId = a.AssetId;n newCase.Origin = a.Origin;n newCase.Reason = a.Reason;n newCase.Date_Due__c = (mapProduct.get(a.Id) != null) ? (Date.today().addDays(Integer.valueOf(mapProduct.get(a.Id)))) : (Date.today());n newCaseList.add(newCase);n }n if(newCaseList.size() > 0){n insert newCaseList;n }n n n } n n}