Advertisement
Learnify_Rectify

Synchronize Salesforce data with an external system

Jul 10th, 2024 (edited)
10,903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | Source Code | 0 0
  1. Synchronize Salesforce data with an external system from (superbadge - Apex Specialist)
  2.  
  3.  
  4. ----------------------------------------------------------------
  5.  
  6. SOURCE CODE : WarehouseCalloutService
  7.  
  8. public with sharing class WarehouseCalloutService implements Queueable {
  9. private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
  10.  
  11. //class that makes a REST callout to an external warehouse system to get a list of equipment that needs to be updated.
  12. //The callout’s JSON response returns the equipment records that you upsert in Salesforce.
  13.  
  14. @future(callout=true)
  15. public static void runWarehouseEquipmentSync(){
  16. Http http = new Http();
  17. HttpRequest request = new HttpRequest();
  18.  
  19. request.setEndpoint(WAREHOUSE_URL);
  20. request.setMethod('GET');
  21. HttpResponse response = http.send(request);
  22.  
  23. List<Product2> warehouseEq = new List<Product2>();
  24.  
  25. if (response.getStatusCode() == 200){
  26. List<Object> jsonResponse = (List<Object>)JSON.deserializeUntyped(response.getBody());
  27. System.debug(response.getBody());
  28.  
  29. //class maps the following fields: replacement part (always true), cost, current inventory, lifespan, maintenance cycle, and warehouse SKU
  30. //warehouse SKU will be external ID for identifying which equipment records to update within Salesforce
  31. for (Object eq : jsonResponse){
  32. Map<String,Object> mapJson = (Map<String,Object>)eq;
  33. Product2 myEq = new Product2();
  34. myEq.Replacement_Part__c = (Boolean) mapJson.get('replacement');
  35. myEq.Name = (String) mapJson.get('name');
  36. myEq.Maintenance_Cycle__c = (Integer) mapJson.get('maintenanceperiod');
  37. myEq.Lifespan_Months__c = (Integer) mapJson.get('lifespan');
  38. myEq.Cost__c = (Integer) mapJson.get('cost');
  39. myEq.Warehouse_SKU__c = (String) mapJson.get('sku');
  40. myEq.Current_Inventory__c = (Double) mapJson.get('quantity');
  41. myEq.ProductCode = (String) mapJson.get('_id');
  42. warehouseEq.add(myEq);
  43. }
  44.  
  45. if (warehouseEq.size() > 0){
  46. upsert warehouseEq;
  47. System.debug('Your equipment was synced with the warehouse one');
  48. }
  49. }
  50. }
  51.  
  52. public static void execute (QueueableContext context){
  53. runWarehouseEquipmentSync();
  54. }
  55.  
  56. }
  57.  
  58. ----------------------------------------------------------------
  59.  
  60.  
  61.  
  62. After saving the code open execute anonymous window with (ctrl+E) , now run this method
  63. -------------------------------------------
  64.  
  65. System.enqueueJob(new WarehouseCalloutService());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement