Apex Callout from Queueable Apex

city

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;

}

}

}

Share on

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Related Posts

LWC vs Aura

LWC LWC uses core Web Components standards (Native browser APIs) and provides only what’s necessary to perform well in browsers supported by Salesforce. Because it’s

Marketing Cloud Basics

It enables you to know your customer, personalize with intelligence, and engage across the entire journey. It’s the only integrated customer engagement platform that enables

SFDX

Prerequisite: Enable “Dev Hub” in your Production Org. 1. Log in to the Dev Hub To authorize the Dev Hub, use the web login flow

Platform Events

Platform event is based on Event-driven architecture. Unlike request-response communication models, software architecture built on an event-driven model decouples event producers from event consumers, thereby