Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.7 kB
2
Indexable
public with sharing class WarehouseCalloutService implements Queueable {
    
    private static final String WAREHOUSE_URL = 'https://th-superbadge-apex.herokuapp.com/equipment';
    
    @future(Callout=true)
    public static void UpdateEquipment(){
        Http h1 = new Http();
        HttpRequest req1 = new HttpRequest();
        req1.setMethod('GET');
        req1.setEndpoint(WAREHOUSE_URL);
        HttpResponse res1 = h1.send(req1);
        
        List<Product2> ToUpdate = new List<product2>();
        
        if (res1.getStatusCode() == 200){
            list<object> Risp = (list<object>) Json.deserializeUntyped(res1.getBody());
            
            for (Object el: Risp){
                Map<String,Object> MapRisp = (Map<String,Object>) el;
                Product2 nuovo = new Product2();
                nuovo.Name = (String) MapRisp.get('name');
                nuovo.Replacement_Part__c = (boolean) MapRisp.get('replacement');
                nuovo.Cost__c = (Integer) MapRisp.get('cost');
                nuovo.Lifespan_Months__c = (Integer) MapRisp.get('lifespan');
                nuovo.Maintenance_Cycle__c = (Integer) MapRisp.get('maintenanceperiod');
                nuovo.Current_Inventory__c = (Integer) MapRisp.get('quantity');
                nuovo.ExternalId = (String) MapRisp.get('sku');
                nuovo.ProductCode = (String) MapRisp.get('_id');
                ToUpdate.add(nuovo);
            }
            
            if (ToUpdate.size() != 0){
                upsert ToUpdate;
            }
        }
    }
    
    public static void execute(QueueableContext context){
		 UpdateEquipment();
    }
}