You need to sign in to do that
Don't have an account?

Apex Specialist Super badge Challenge2: Synchronize Salesforce data with an external system using asynchronous REST callouts, Can anyone explain the steps to implement logic?
What are the basic steps to implement ? when do we need JSON?I know many of you posted code , but please explain me the steps in sequence in order to implement logic.
Can you please provide those kind steps to resolve challenge2 : Synchronize Salesforce data with an external system.
Thanks again,
Rajani
Please follow these steps for Apex Specialist Challenge 2 - Synchronize Salesforce data with an external system
First create In Quick Box -> Setting -> Remote Site -> New
Remote Site Name : th_superbadge
Remote Site URL : https://th-superbadge-apex.herokuapp.com
Developer Console : WarehouseCalloutService .apxc:
WarehouseCalloutService.apxc
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;
}
}
WarehouseCalloutServiceTest.apxc
@isTest
private class WarehouseCalloutServiceTest {
// implement your mock callout test here
@isTest
static void WarehouseEquipmentSync(){
Test.startTest();
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new WarehouseCalloutServiceMock());
// This causes a fake response to be sent from the class that implements HttpCalloutMock.
WarehouseCalloutService.runWarehouseEquipmentSync();
Test.stopTest();
System.assertEquals(1, [SELECT count() FROM Product2]);
}
}
Open anonymous window and execute code once before check challenge:
WarehouseCalloutService.runWarehouseEquipmentSync();
Thanks.
Please check answer at:
https://developer.salesforce.com/forums/?id=9060G0000005YMlQAM
Use ----------- System.enqueueJob(New WarehouseCalloutService()); in Anonymous Window instead WarehouseCalloutService.runWarehouseEquipmentSync();