Implement the Interface “Database.AllowsCallout” to do a callout from Queueable Apex. If you don’t implement this interface, below error is showed
Callout not allowed from this future method. Please enable callout by annotating the future method. eg: @Future(callout=true)
Solution
public with sharing class WarehouseCalloutService implements Queueable, Database.AllowsCallouts{
private static final String WAREHOUSE_URL = ‘https://th-superbadge-apex.herokuapp.com/equipment’;
public void execute(QueueableContext context) {
HttpRequest request = new HttpRequest();
request.setEndpoint(WAREHOUSE_URL);
request.setMethod(‘GET’);
Http http = new Http();
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200){
List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
List<Product2> prods = new List<Product2>();
for(Object res : results){
Map<String, Object>productMap = (Map<String, Object>)res;
prods.add(new Product2( name = (String)productMap.get(‘name’), Replacement_Part__c = true, Cost__c = (Decimal)productMap.get(‘cost’), Current_Inventory__c = (Decimal)productMap.get(‘quantity’),
Lifespan_Months__c = (Decimal)productMap.get(‘lifespan’), Maintenance_Cycle__c = (Decimal)productMap.get(‘maintenanceperiod’),
Warehouse_SKU__c = (String)productMap.get(‘sku’)));
}
if(!prods.isEmpty()) upsert prods;
}
}
}