Table of Contents |
---|
Challenge 1: Automate record creation Challenge 2: Synchronize Salesforce data with an external system Challenge 3: Schedule synchronization Challenge 4: Test automation logic Challenge 5: Test callout logic Challenge 6: Test scheduling logic |
Superbadge Apex Specialist looks good on Resume, and they prove worth it as well. You should definitely complete the task on your own and you can get all the help. All these codes are working 100% and run smoothly to help you achieve the below shiny badge. I had mine and wrote this while doing so…
Free Salesforce Exam Coupon Codes for 2022
Apex Specialist
What You’ll Be Doing to Earn This Superbadge
- Automate record creation using Apex triggers
- Synchronize Salesforce data with an external system using asynchronous REST callouts
- Schedule synchronization using Apex code
- Test automation logic to confirm Apex trigger side effects
- Test integration logic using callout mocks
- Test scheduling logic to confirm action gets queued
Challenge 1: Automate record creation
Stuck on Superbadge Apex Specialist Step 1?
Change the labels for Case and Product To Maintenance Request and Equipment respectively.
ChangethelabelsforStandard Objectsand Fields in Salesforce
Go to Setup -> Customize -> Tab Names andLabels->RenameTabs andLabels.
Choose the Tab (orObject) you want torenameand clickEdit.
Change the Codes
Go to Developer console and edit the Apex class and related triggers for below:
public with sharing class MaintenanceRequestHelper { public static void updateWorkOrders(List<Case> caseList) { List<case> newCases = new List<Case>(); Map<String,Integer> result=getDueDate(caseList); for(Case c : caseList){ if(c.status=='closed') if(c.type=='Repair' || c.type=='Routine Maintenance'){ Case newCase = new Case(); newCase.Status='New'; newCase.Origin='web'; newCase.Type='Routine Maintenance'; newCase.Subject='Routine Maintenance of Vehicle'; newCase.Vehicle__c=c.Vehicle__c; newCase.Equipment__c=c.Equipment__c; newCase.Date_Reported__c=Date.today(); if(result.get(c.Id)!=null) newCase.Date_Due__c=Date.today()+result.get(c.Id); else newCase.Date_Due__c=Date.today(); newCases.add(newCase); } } insert newCases; } // public static Map<String,Integer> getDueDate(List<case> CaseIDs){ Map<String,Integer> result = new Map<String,Integer>(); Map<Id, case> caseKeys = new Map<Id, case> (CaseIDs); List<AggregateResult> wpc=[select Maintenance_Request__r.ID cID,min(Equipment__r.Maintenance_Cycle__c)cycle from Work_Part__c where Maintenance_Request__r.ID in :caseKeys.keySet() group by Maintenance_Request__r.ID ]; for(AggregateResult res :wpc){ Integer addDays=0; if(res.get('cycle')!=null) addDays+=Integer.valueOf(res.get('cycle')); result.put((String)res.get('cID'),addDays); } return result;}}
trigger MaintenanceRequest on Case (before update, after update) { // ToDo: Call MaintenanceRequestHelper.updateWorkOrders if(Trigger.isAfter) MaintenanceRequestHelper.updateWorkOrders(Trigger.New); }
Challenge 2: Synchronize Salesforce data with an external system
Issue with Superbadge Apex Specialist Step 2?
public with sharing class WarehouseCalloutService { private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment'; @future(callout=true) public static void runWarehouseEquipmentSync() { //ToDo: complete this method to make the callout (using @future) to the // REST endpoint and update equipment on hand. HttpResponse response = getResponse(); if(response.getStatusCode() == 200) { List<Product2> results = getProductList(response); //get list of products from Http callout response if(results.size() >0) upsert results Warehouse_SKU__c; //Upsert the products in your org based on the external ID SKU } } //Get the product list from the external link public static List<Product2> getProductList(HttpResponse response) { List<Object> externalProducts = (List<Object>) JSON.deserializeUntyped(response.getBody()); //desrialize the json response List<Product2> newProducts = new List<Product2>(); for(Object p : externalProducts) { Map<String, Object> productMap = (Map<String, Object>) p; Product2 pr = new Product2(); //Map the fields in the response to the appropriate fields in the Equipment object pr.Replacement_Part__c = (Boolean)productMap.get('replacement'); pr.Cost__c = (Integer)productMap.get('cost'); pr.Current_Inventory__c = (Integer)productMap.get('quantity'); pr.Lifespan_Months__c = (Integer)productMap.get('lifespan') ; pr.Maintenance_Cycle__c = (Integer)productMap.get('maintenanceperiod'); pr.Warehouse_SKU__c = (String)productMap.get('sku'); pr.ProductCode = (String)productMap.get('_id'); pr.Name = (String)productMap.get('name'); newProducts.add(pr); } return newProducts; } // Send Http GET request and receive Http response public static HttpResponse getResponse() { Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint(WAREHOUSE_URL); request.setMethod('GET'); HttpResponse response = http.send(request); return response; } }
Execute below in the anonymous window :
WarehouseCalloutService.runWarehouseEquipmentSync();
Wait for a minute and run it twice maybe before checking challenges.
Challenge 3: Schedule synchronization
Help with Superbadge Apex Specialist Step 3?
Go to Setup > Apex Classes > Schedule a job like below:
Edit the following in the Developer console
global class WarehouseSyncSchedule implements Schedulable{ // implement scheduled code here global void execute (SchedulableContext sc){ WarehouseCalloutService.runWarehouseEquipmentSync(); //optional this can be done by debug mode String sch = '00 00 01 * * ?';//on 1 pm System.schedule('WarehouseSyncScheduleTest', sch, new WarehouseSyncSchedule()); }}
And execute in the anonymous window below:
WarehouseSyncSchedule scheduleInventoryCheck();
Read More: Salesforce Interview Questions and Answers We Swear By!
Challenge 4: Test automation logic
Stuck on Superbadge Apex Specialist Step 4?
Modifications to the below Apex Classes as below.
trigger MaintenanceRequest on Case (before update, after update) { if(Trigger.isUpdate && Trigger.isAfter) MaintenanceRequestHelper.updateWorkOrders(Trigger.New);}
@IsTestprivate class InstallationTests { private static final String STRING_TEST = 'TEST'; private static final String NEW_STATUS = 'New'; private static final String WORKING = 'Working'; private static final String CLOSED = 'Closed'; private static final String REPAIR = 'Repair'; private static final String REQUEST_ORIGIN = 'Web'; private static final String REQUEST_TYPE = 'Routine Maintenance'; private static final String REQUEST_SUBJECT = 'AMC Spirit'; public static String CRON_EXP = '0 0 1 * * ?'; static testmethod void testMaintenanceRequestNegative() { Vehicle__c vehicle = createVehicle(); insert vehicle; Id vehicleId = vehicle.Id; Product2 equipment = createEquipment(); insert equipment; Id equipmentId = equipment.Id; Case r = createMaintenanceRequest(vehicleId, equipmentId); insert r; Work_Part__c w = createWorkPart(equipmentId, r.Id); insert w; Test.startTest(); r.Status = WORKING; update r; Test.stopTest(); List<case> allRequest = [SELECT Id FROM Case]; Work_Part__c workPart = [SELECT Id FROM Work_Part__c WHERE Maintenance_Request__c =: r.Id]; System.assert(workPart != null); System.assert(allRequest.size() == 1); } static testmethod void testWarehouseSync() { Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock()); Test.startTest(); String jobId = System.schedule('WarehouseSyncSchedule', CRON_EXP, new WarehouseSyncSchedule()); CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId]; System.assertEquals(CRON_EXP, ct.CronExpression); System.assertEquals(0, ct.TimesTriggered); Test.stopTest(); } private static Vehicle__c createVehicle() { Vehicle__c v = new Vehicle__c(Name = STRING_TEST); return v; } private static Product2 createEquipment() { Product2 p = new Product2(Name = STRING_TEST, Lifespan_Months__c = 10, Maintenance_Cycle__c = 10, Replacement_Part__c = true); return p; } private static Case createMaintenanceRequest(Id vehicleId, Id equipmentId) { Case c = new Case(Type = REPAIR, Status = NEW_STATUS, Origin = REQUEST_ORIGIN, Subject = REQUEST_SUBJECT, Equipment__c = equipmentId, Vehicle__c = vehicleId); return c; } private static Work_Part__c createWorkPart(Id equipmentId, Id requestId) { Work_Part__c wp = new Work_Part__c(Equipment__c = equipmentId, Maintenance_Request__c = requestId); return wp; }}
public with sharing class MaintenanceRequestHelper {public static void updateWorkOrders(List<case> caseList) {List<case> newCases = new List<case>();Map<String,Integer> result=getDueDate(caseList);for(Case c : caseList){if(c.status=='closed')if(c.type=='Repair' || c.type=='Routine Maintenance'){Case newCase = new Case();newCase.Status='New';newCase.Origin='web';newCase.Type='Routine Maintenance';newCase.Subject='Routine Maintenance of Vehicle';newCase.Vehicle__c=c.Vehicle__c;newCase.Equipment__c=c.Equipment__c;newCase.Date_Reported__c=Date.today();if(result.get(c.Id)!=null)newCase.Date_Due__c=Date.today()+result.get(c.Id);elsenewCase.Date_Due__c=Date.today();newCases.add(newCase);}}insert newCases;}//public static Map<String,Integer> getDueDate(List<case> CaseIDs){Map<String,Integer> result = new Map<String,Integer>();Map<Id, case> caseKeys = new Map<Id, case> (CaseIDs);List<aggregateresult> wpc=[select Maintenance_Request__r.ID cID,min(Equipment__r.Maintenance_Cycle__c)cyclefrom Work_Part__c where Maintenance_Request__r.ID in :caseKeys.keySet() group by Maintenance_Request__r.ID ];for(AggregateResult res :wpc){Integer addDays=0;if(res.get('cycle')!=null)addDays+=Integer.valueOf(res.get('cycle'));result.put((String)res.get('cID'),addDays);}return result;}}
@isTestpublic class MaintenanceRequestTest { static List<case> caseList1 = new List<case>(); static List<product2> prodList = new List<product2>(); static List<work_part__c> wpList = new List<work_part__c>();@testSetup static void getData(){ caseList1= CreateData( 300,3,3,'Repair'); } public static List<case> CreateData( Integer numOfcase, Integer numofProd, Integer numofVehicle, String type){ List<case> caseList = new List<case>(); //Create Vehicle Vehicle__c vc = new Vehicle__c(); vc.name='Test Vehicle'; upsert vc; //Create Equiment for(Integer i=0;i<numofProd;i++){ Product2 prod = new Product2(); prod.Name='Test Product'+i; if(i!=0) prod.Maintenance_Cycle__c=i; prod.Replacement_Part__c=true; prodList.add(prod); } upsert prodlist; //Create Case for(Integer i=0;i< numOfcase;i++){ Case newCase = new Case(); newCase.Status='New'; newCase.Origin='web'; if( math.mod(i, 2) ==0) newCase.Type='Routine Maintenance'; else newCase.Type='Repair'; newCase.Subject='Routine Maintenance of Vehicle' +i; newCase.Vehicle__c=vc.Id; if(i<numofProd) newCase.Equipment__c=prodList.get(i).ID; else newCase.Equipment__c=prodList.get(0).ID; caseList.add(newCase); } upsert caseList; for(Integer i=0;i<numofProd;i++){ Work_Part__c wp = new Work_Part__c(); wp.Equipment__c =prodlist.get(i).Id ; wp.Maintenance_Request__c=caseList.get(i).id; wplist.add(wp) ; } upsert wplist; return caseList; } public static testmethod void testMaintenanceHelper(){ Test.startTest(); getData(); for(Case cas: caseList1) cas.Status ='Closed'; update caseList1; Test.stopTest(); }}
Challenge 5: Test callout logic
Issue with Superbadge Apex Specialist Step 5?
Modify the Apex Classes as below, save and run all.
@IsTestprivate class WarehouseCalloutServiceTest { // implement your mock callout test here @isTest static void testWareHouseCallout(){ Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock()); WarehouseCalloutService.runWarehouseEquipmentSync(); }}
@isTestpublic class WarehouseCalloutServiceMock implements HTTPCalloutMock { // implement http mock callout public HTTPResponse respond (HttpRequest request){ HttpResponse response = new HTTPResponse(); response.setHeader('Content-type','application/json'); response.setBody('[{"_id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"},{"_id":"55d66226726b611100aaf742","replacement":true,"quantity":183,"name":"Cooling Fan","maintenanceperiod":0,"lifespan":0,"cost":300,"sku":"100004"},{"_id":"55d66226726b611100aaf743","replacement":true,"quantity":143,"name":"Fuse 20A","maintenanceperiod":0,"lifespan":0,"cost":22,"sku":"100005"}]'); response.setStatusCode(200); return response; }}
Challenge 6: Test scheduling logic
Stuck on Superbadge Apex Specialist Step 6?
Modify as below
@isTestprivate class WarehouseSyncScheduleTest { public static String CRON_EXP = '0 0 0 15 3 ? 2022'; static testmethod void testjob(){ MaintenanceRequestTest.CreateData( 5,2,2,'Repair'); Test.startTest(); Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock()); String joBID= System.schedule('TestScheduleJob', CRON_EXP, new WarehouseSyncSchedule()); // List<Case> caselist = [Select count(id) from case where case] Test.stopTest(); }}
Hope this helps!
Looking For? Superbadge Process Automation Specialist – Full Solutions
Share to someone in the Trailhead Community. Let’s grow together.
Need help on specific errors? Start a discussion in the forum to get straight-up answers.
Let the universe renounce some goodness to you 😇
Read More: Salesforce Interview Questions and Answers We Swear By!