Apex Specialist Superbadge

Apex Specialist Superbadges - Shiv Shankar - Medium

From this blog post, am providing brief info about Apex Specialist Super Badge and solutions for all challenges to get Success.

Apex Specialist super badge is prepared by trailhead in the way to become expert in Apex programming level and this badge is compulsory for Advance Apex super badge and Pd-2 certification.

Apex Specialist

What You’ll Be Doing to Earn This Superbadge

  1. Automate record creation using Apex triggers
  2. Synchronize Salesforce data with an external system using asynchronous REST callouts
  3. Schedule synchronization using Apex code
  4. Test automation logic to confirm Apex trigger side effects
  5. Test integration logic using callout mocks
  6. Test scheduling logic to confirm action gets queued

Concepts Tested in This Superbadge

  • Apex Triggers
  • Asynchronous Apex
  • Apex Integration
  • Apex Testing

apex specialist

After we completed all units then we need to take challenges to get badge for that we need to do some pre-work.

Pre Requisites & Tips:

  1. Create a new Trailhead Playground for this superbadge. Using this org for any other reason might create problems when validating the challenge.
  2. Install this unmanaged package (package ID: 04tB0000000Q3Cq). This package contains all schema and shells for Apex logic needed to complete this challenge. The only changes you need to make to the data schema are renaming the standard objects or modifying standard fields per the Entity Diagram. If you have trouble installing a managed or unmanaged package or app from AppExchange, follow the steps in this article.
  3. Use the naming conventions specified in the requirements document to ensure a successful deployment.
  4. Review the data schema in your modified Salesforce org as you read the detailed requirements below.
  5. Create a few records of the Vehicle and Maintenance Request  with associated work parts and equipment to understand the entity flow , then the challenges are pretty straightforward to implement
  6. This challenge also test on usage of coding best practices like bulkify your code , use helper classin triggere , no DML statements in a loop to name a few.

First Challenge (Automate Record Creation )

Write a MaintenanceRequest Trigger on case object.

trigger MaintenanceRequest on Case (before update, after update) {

MaintenanceRequestHelper.updateWorkOrders(Trigger.New);
}

Write an Apex helper class for the same ‘MaintenanceRequestHelper’

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;
}

}

Second Challenge (Synchronize Salesforce data with an external system)

Write a with sharing WarehouseCalloutService class to handle Rest callout.

public with sharing class WarehouseCalloutService {
private static final String WAREHOUSE_URL = ‘https://th-superbadge-apex.herokuapp.com/equipment&#8217;;
@future(callout=true)
public static void runWarehouseEquipmentSync() {
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(WAREHOUSE_URL);
req.setMethod(‘GET’);
HttpResponse res = http.send(req);
List<Product2> equipList = new List<Product2>();
if(res.getStatusCode()==200){
List< Object> result =(List<Object>)JSON.deserializeUntyped(res.getBody());
for(Object prodMap: result){
Map<String,Object> prod = (Map<String,Object>)prodMap;
Product2 newprod = new Product2();
newprod.ExternalId=(String)prod.get(‘_id’);
newprod.Replacement_Part__c=true;
newprod.Cost__c=(Decimal)prod.get(‘cost’);
newprod.Current_Inventory__c=(Decimal)prod.get(‘quantity’);
newprod.Lifespan_Months__c=(Double)prod.get(‘lifespan’);
newprod.Name=(String)prod.get(‘name’);
newprod.Maintenance_Cycle__c=(Integer)prod.get(‘maintenanceperiod’);
newprod.ExternalId=(String)prod.get(‘sku’);
equipList.add(newprod);
}
List< Database.SaveResult> sr = Database.update(equipList, dml);
}
}

}

Third Challenge(Schedule synchronization)

Go to Salesforce Setup and Create a Schedule job ScheduleWareHouseSync.

public with sharing class WarehouseSyncSchedule implements Schedulable {
// implement scheduled code here
public void execute(SchedulableContext sc){
WarehouseCalloutService.runWarehouseEquipmentSync();
}

}

Fourth Challenge (Test automation logic)

Write a test class ‘MaintenanceRequestTest ‘ to confirm Apex trigger logic.

@isTest
public  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();
}

}

Fifth Challenge(Test callout logic)

 

In the 5th challange write a test integration logic using callout mock

@isTest
public 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;
}
}
@IsTest
private class WarehouseCalloutServiceTest {
// implement your mock callout test here
@isTest
static void testWareHouseCallout(){
Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock());
WarehouseCalloutService.runWarehouseEquipmentSync();
}
}

Sixth challenge (Test scheduling logic)

Scheduling logic to action gets queued.

@isTest
private 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();
}
}

Superbadge Complete!

+5000 Points

similarly

2 thoughts on “Apex Specialist Superbadge

  1. Pingback: Process Automation Super Badge – Jayakrishna Ganjikunta

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s