Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Synchronize Salesforce data with an external system from (superbadge - Apex Specialist)
- ----------------------------------------------------------------
- SOURCE CODE : WarehouseCalloutService
- public with sharing class WarehouseCalloutService implements Queueable {
- private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
- //class that makes a REST callout to an external warehouse system to get a list of equipment that needs to be updated.
- //The callout’s JSON response returns the equipment records that you upsert in Salesforce.
- @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);
- List<Product2> warehouseEq = new List<Product2>();
- if (response.getStatusCode() == 200){
- List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
- System.debug(response.getBody());
- //class maps the following fields: replacement part (always true), cost, current inventory, lifespan, maintenance cycle, and warehouse SKU
- //warehouse SKU will be external ID for identifying which equipment records to update within Salesforce
- for (Object eq : jsonResponse){
- Map<String,Object> mapJson = (Map<String,Object>)eq;
- Product2 myEq = new Product2();
- myEq.Replacement_Part__c = (Boolean) mapJson.get('replacement');
- myEq.Name = (String) mapJson.get('name');
- myEq.Maintenance_Cycle__c = (Integer) mapJson.get('maintenanceperiod');
- myEq.Lifespan_Months__c = (Integer) mapJson.get('lifespan');
- myEq.Cost__c = (Integer) mapJson.get('cost');
- myEq.Warehouse_SKU__c = (String) mapJson.get('sku');
- myEq.Current_Inventory__c = (Double) mapJson.get('quantity');
- myEq.ProductCode = (String) mapJson.get('_id');
- warehouseEq.add(myEq);
- }
- if (warehouseEq.size() > 0){
- upsert warehouseEq;
- System.debug('Your equipment was synced with the warehouse one');
- }
- }
- }
- public static void execute (QueueableContext context){
- runWarehouseEquipmentSync();
- }
- }
- ----------------------------------------------------------------
- After saving the code open execute anonymous window with (ctrl+E) , now run this method
- -------------------------------------------
- System.enqueueJob(new WarehouseCalloutService());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement