Skip to content

Instantly share code, notes, and snippets.

@dylanmensaert
Created March 17, 2019 19:41
Show Gist options
  • Select an option

  • Save dylanmensaert/87c2c98120e4d6a0de4f5cff287f96ac to your computer and use it in GitHub Desktop.

Select an option

Save dylanmensaert/87c2c98120e4d6a0de4f5cff287f96ac to your computer and use it in GitHub Desktop.
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(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(WAREHOUSE_URL);
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
// System.assert(false, response.getBody());
List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
// Cast the values in the 'animals' key as a list
System.debug('Received the following animals:');
List<Product2> products = new list<Product2>();
for (Object animal: results) {
Map<String, Object> other = (Map<String, Object>)animal;
Product2 p = new Product2(
Name = String.valueOf(other.get('name')),
Lifespan_Months__c = INteger.valueOf(other.get('lifespan')),
Maintenance_Cycle__c = INteger.valueOf(other.get('maintenanceperiod')),
Replacement_Part__c= true,//Boolean.valueOf(other.get('replacement')),
Warehouse_SKU__c = String.valueOf(other.get('sku')),
Current_Inventory__c = INteger.valueOf(other.get('quantity')),
Cost__c = INteger.valueOf(other.get('cost'))
);
products.add(p);
}
upsert products Warehouse_SKU__c;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment