Synchronize Salesforce data with an external system || Apex Specialist || Superbadge || Challenge 3

Поделиться
HTML-код
  • Опубликовано: 11 дек 2024

Комментарии • 1

  • @TrailheadTitans
    @TrailheadTitans  10 месяцев назад +1

    Challenge 3:- WarehouseCalloutService
    -----------------------------------------------------------------
    public with sharing class WarehouseCalloutService implements Queueable {
    private static final String WAREHOUSE_URL = '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 warehouseEq = new List();

    if (response.getStatusCode() == 200){
    List jsonResponse = (List)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 mapJson = (Map)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 ( CTRl+E ) and run this method ,
    -------------------------------------------
    System.enqueueJob(new WarehouseCalloutService());